加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > asp.Net > 正文

Asp.Net在Sql中选择

发布时间:2020-12-16 09:14:12 所属栏目:asp.Net 来源:网络整理
导读:我知道这将非常简单.我在asp.net中看到了很多不同的方法,没有真正的标准.我想知道的是如何从asp.net中的sql数据库中干净地选择并检索多个记录.例如:选择所有用户ID. String sql = "SELECT [UserId] FROM [UserProfiles] WHERE NOT [UserId] = 'CurrentUserI
我知道这将非常简单.我在asp.net中看到了很多不同的方法,没有真正的标准.我想知道的是如何从asp.net中的sql数据库中干净地选择并检索多个记录.例如:选择所有用户ID.

String sql = 
  "SELECT [UserId] FROM [UserProfiles] WHERE NOT [UserId] = 'CurrentUserId'";

string strCon = System.Web
                      .Configuration
                      .WebConfigurationManager
                      .ConnectionStrings["SocialSiteConnectionString"]
                      .ConnectionString;

SqlConnection conn = new SqlConnection(strCon);
SqlCommand comm = new SqlCommand(sql,conn);
conn.Open();

/*
 This is where I need to know how to retrieve the information from the
 above command(comm). I am looking for something similiar to php's
 mysql_result. I want to access the records kind of like an array or some
 other form of retrieving all the data.
 Also when the new SqlCommand is called...does that actual run the
 SELECT STATEMENT or is there another step. 
*/

conn.Close();

解决方法

我认为这就是你要找的东西.

String sql = "SELECT [UserId] FROM [UserProfiles] WHERE NOT [UserId] = 'CurrentUserId'";

string strCon = System.Web
                      .Configuration
                      .WebConfigurationManager
                      .ConnectionStrings["SocialSiteConnectionString"].ConnectionString;

SqlConnection conn = new SqlConnection(strCon);
SqlCommand comm = new SqlCommand(sql,conn);
conn.Open();
SqlDataReader nwReader = comm.ExecuteReader();
while (nwReader.Read())
{
    int UserID = (int)nwReader["UserID"];
    // Do something with UserID here...
}
nwReader.Close();
conn.Close();

不过,我不得不说整体方法可以使用很多调整.首先,您至少可以从简化对ConnectionString的访问开始.例如,您可以将以下内容添加到Global.asax.cs文件中:

using System;
   using System.Configuration;

   public partial class Global : HttpApplication
   {
      public static string ConnectionString; 

      void Application_Start(object sender,EventArgs e)
      { 
          ConnectionString = ConfigurationManager.ConnectionStrings["SocialSiteConnectionString"].ConnectionString;
      }
      ...
    }

现在,在整个代码中,只需使用以下代码访问它:

SqlConnection conn = new SqlConnection(Global.ConnectionString);

更好的是,创建一个隐藏“管道”的类.要在我的代码中运行相同的查询,我只需输入:

using (BSDIQuery qry = new BSDIQuery())
        {
            SqlDataReader nwReader = qry.Command("SELECT...").ReturnReader();
            // If I needed to add a parameter I'd add it above as well: .ParamVal("CurrentUser")
            while (nwReader.Read())
            {
                int UserID = (int)nwReader["UserID"];
                // Do something with UserID here...
            }
            nwReader.Close();
        }

这只是使用我的DAL的一个例子.但是,请注意,没有连接字符串,没有创建或管理命令或连接对象,只是“BSDIQuery”(除了显示的内容之外,它还会执行许多不同的操作).您的方法会因您最常做的任务而有所不同.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读