i want the statment of how to use the retrieved data from sql database table, i mean i want to use the last cell of two columns from the same table with c#
我想要如何使用从sql数据库表中检索的数据的声明,我的意思是我想使用c#中同一个表中两列的最后一个单元格
2 个解决方案
#1
Suppose your table has following columns:
假设您的表有以下列:
- ID
- Name
You can access second column "Name" as follows:
您可以访问第二列“名称”,如下所示:
DataTable dtb=....;//Here your code returns DataTable
//suppose you want to set value of second column (Name) from 1st row on a label
label1.Text = dtb.Rows[0][1];
//OR
label1.Text = dtb.Rows[0]["Name"];
//OR if you want to use second column from all rows, say in a list
foreach(DataRow dr in dtb.Rows)
{
list1.Items.Add(dr["Name"]);
}
#2
Using a SqlDataReader:
使用SqlDataReader:
using (SqlDataReader reader = command.ExecuteReader()) {
while (reader.Read()) {
int id = (int)reader["ID"]; // or reader.GetInt(0);
string name = reader["Name"].ToString(); // or reader.GetString(1);
}
reader.Close();
}
#1
Suppose your table has following columns:
假设您的表有以下列:
- ID
- Name
You can access second column "Name" as follows:
您可以访问第二列“名称”,如下所示:
DataTable dtb=....;//Here your code returns DataTable
//suppose you want to set value of second column (Name) from 1st row on a label
label1.Text = dtb.Rows[0][1];
//OR
label1.Text = dtb.Rows[0]["Name"];
//OR if you want to use second column from all rows, say in a list
foreach(DataRow dr in dtb.Rows)
{
list1.Items.Add(dr["Name"]);
}
#2
Using a SqlDataReader:
使用SqlDataReader:
using (SqlDataReader reader = command.ExecuteReader()) {
while (reader.Read()) {
int id = (int)reader["ID"]; // or reader.GetInt(0);
string name = reader["Name"].ToString(); // or reader.GetString(1);
}
reader.Close();
}