我知道这将非常简单.我在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();
Mark Brittin.. 6
我认为这就是你要找的东西.
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"(除了显示的内容之外,它还会执行许多不同的操作).您的方法会因您最常做的任务而有所不同.
我认为这就是你要找的东西.
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"(除了显示的内容之外,它还会执行许多不同的操作).您的方法会因您最常做的任务而有所不同.