C# SQL Server:返回表中的所有行并读取它们的值
我有一个6列5行的表。我想选择所有行并使用SqlDataReader ASP.NET C读取它们。我目前可以使用dataReader[columnName].ToString访问单个列,并将其存储在字符串变量中 我想逐行通读这些列 谢谢你的帮助 基于,下面是一个简单的实现:C# SQL Server:返回表中的所有行并读取它们的值,c#,sql,asp.net,sql-server,C#,Sql,Asp.net,Sql Server,我有一个6列5行的表。我想选择所有行并使用SqlDataReader ASP.NET C读取它们。我目前可以使用dataReader[columnName].ToString访问单个列,并将其存储在字符串变量中 我想逐行通读这些列 谢谢你的帮助 基于,下面是一个简单的实现: private static void ReadGetOrdinal(string connectionString) { string queryString = "SELECT DISTI
private static void ReadGetOrdinal(string connectionString)
{
string queryString = "SELECT DISTINCT CustomerID FROM dbo.Orders;";
using (SqlConnection connection = new SqlConnection(connectionString))
{
using(SqlCommand command = new SqlCommand(queryString, connection))
{
connection.Open();
using (SqlDataReader reader = command.ExecuteReader())
{
// Call GetOrdinal and assign value to variable.
int customerID = reader.GetOrdinal("CustomerID");
// Use variable with GetString inside of loop.
while (reader.Read())
{
Console.WriteLine("CustomerID={0}", reader.GetString(customerID));
}
}
}
}
}
如果要将行和列存储到某种类型的集合中,可以尝试使用列表和字典,这样可以根据需要添加任意多的行
List<Dictionary<string, string>> rows = new List<Dictionary<string, string>>();
Dictionary<string, string> column;
string sqlQuery = "SELECT USER_ID, FIRSTNAME, LASTNAME FROM USERS";
SqlCommand command = new SqlCommand(sqlQuery, myConnection);
try
{
myConnection.Open();
SqlDataReader reader = command.ExecuteReader();
while (reader.Read())
{ //Every new row will create a new dictionary that holds the columns
column = new Dictionary<string, string>();
column["USER_ID"] = reader["USER_ID"].ToString();
column["FIRSTNAME"] = reader["FIRSTNAME"].ToString();
column["LASTNAME"] = reader["LASTNAME"].ToString();
rows.Add(column); //Place the dictionary into the list
}
reader.Close();
}
catch (Exception ex)
{
//If an exception occurs, write it to the console
Console.WriteLine(ex.ToString());
}
finally
{
myConnection.Close();
}
//Once you've read the rows into the collection you can loop through to
//display the results
foreach(Dictionary<string, string> column in rows)
{
Console.Write(column["USER_ID"]) + " ";
Console.Write(column["FIRSTNAME"] + " ";
Console.Write(column["LASTNAME"] + " ";
Console.WriteLine();
}
给你
成员变量:
static SqlConnection moConnection = new SqlConnection("Data Source=XXX;Initial Catalog=XXX;User ID=XXX;Password=XXX");
static SqlCommand moCommand = new SqlCommand();
static SqlDataAdapter moAdapter = new SqlDataAdapter();
static DataSet moDataSet = new DataSet();
static string msQuery;
在类方法中,您必须编写以下代码:
DataSet loDataSet = new DataSet();
msQuery = "SELECT * FROM [TableName]";
Execommand(msQuery);
moAdapter.Fill(loDataSet);
DataTable loTable = new DataTable();
loTable = loDataSet.Tables[0];
if (loTable != null && loTable.Rows.Count > 0)
{
foreach (DataRow foRow in loTable.Rows)
{
string lsUserID = Convert.ToString(foRow["UserID"]);
}
}
谢谢你们大家的帮助。我回答这个问题是为了反映我的经验。我使用了GridView,它是ASP.NET的标准控件。它真的很容易完成这项工作,而且高度可定制 下面添加了一个示例代码,以进一步说明其工作原理。如果有人在使用它时需要帮助,请随意在这里发布,我会尽力帮助
<asp:GridView ID="GridView1" runat="server" AllowPaging="True"
AllowSorting="True" AutoGenerateColumns="False" DataKeyNames="CustomerID"
DataSourceID="SqlDataSource1">
<Columns>
<asp:BoundField DataField="CustomerID" HeaderText="CustomerID" ReadOnly="True"
SortExpression="CustomerID" />
<asp:BoundField DataField="CompanyName" HeaderText="CompanyName"
SortExpression="CompanyName" />
<asp:BoundField DataField="ContactName" HeaderText="ContactName"
SortExpression="ContactName" />
<asp:BoundField DataField="ContactTitle" HeaderText="ContactTitle"
SortExpression="ContactTitle" />
<asp:BoundField DataField="Address" HeaderText="Address"
SortExpression="Address" />
<asp:BoundField DataField="City" HeaderText="City" SortExpression="City" />
<asp:BoundField DataField="Region" HeaderText="Region"
SortExpression="Region" />
<asp:BoundField DataField="PostalCode" HeaderText="PostalCode"
SortExpression="PostalCode" />
<asp:BoundField DataField="Country" HeaderText="Country"
SortExpression="Country" />
<asp:BoundField DataField="Phone" HeaderText="Phone" SortExpression="Phone" />
<asp:BoundField DataField="Fax" HeaderText="Fax" SortExpression="Fax" />
</Columns>
</asp:GridView>
你能发布一些你尝试过的代码吗?使用dataReader.Read进行while循环,然后通过for循环中的索引或列名对当前行中的每一列进行索引。下面是MSDN页面供阅读:使用Dapper.NET或Entity Framework之类的ORM,您根本不必摆弄所有那些非常无聊、非常容易出错的低级代码-它们会为您处理所有这些混乱的细节,并返回一个漂亮的,可用的.NET对象或对象列表…您应该将SqlCommand和SqlDataReader放入使用。。。{…}块也是!完全正确@marc_s。也意味着读者。关闭变得多余