将列值添加到 reader asp.net
Add column values to reader asp.net
我想将我所有的 ID 值从 sql 数据库存储到 reader。
到目前为止我得到了:
string strConnString = "Data Source = 'PAULO'; Initial Catalog=ShoppingCartDB;Integrated Security =True";
string str;
SqlCommand com;
protected void Page_Load(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection(strConnString);
con.Open();
str = "select * from CustomerDetails Where CustomerName = '"+Session["New"].ToString()+"'";
com = new SqlCommand(str, con);
SqlDataReader reader = com.ExecuteReader();
while (reader.Read())
{
ListofId.Add(reader["Id"].ToString());
}
ListofId 出错,我缺少什么或可能需要声明什么?
谢谢
如果 id 列表是字符串那么你可以使用 List<string>
if int List<int>
List<string> Listofids = new List<string>();
SqlDataReader reader = com.ExecuteReader();
while (reader.Read())
{
Listofids .Add(reader["Id"].ToString());
}
根据以上评论,您似乎只是错过了为 List 对象创建实例。
很简单,只需声明如下代码即可解决您的问题
List<string> ListofId= new List<string>();
如果解决了您的问题,请告诉我
谢谢
我想将我所有的 ID 值从 sql 数据库存储到 reader。
到目前为止我得到了:
string strConnString = "Data Source = 'PAULO'; Initial Catalog=ShoppingCartDB;Integrated Security =True";
string str;
SqlCommand com;
protected void Page_Load(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection(strConnString);
con.Open();
str = "select * from CustomerDetails Where CustomerName = '"+Session["New"].ToString()+"'";
com = new SqlCommand(str, con);
SqlDataReader reader = com.ExecuteReader();
while (reader.Read())
{
ListofId.Add(reader["Id"].ToString());
}
ListofId 出错,我缺少什么或可能需要声明什么? 谢谢
如果 id 列表是字符串那么你可以使用 List<string>
if int List<int>
List<string> Listofids = new List<string>();
SqlDataReader reader = com.ExecuteReader();
while (reader.Read())
{
Listofids .Add(reader["Id"].ToString());
}
根据以上评论,您似乎只是错过了为 List 对象创建实例。
很简单,只需声明如下代码即可解决您的问题
List<string> ListofId= new List<string>();
如果解决了您的问题,请告诉我
谢谢