在下面的代码中,我试图将reader(r33)分配到数组中,这样我就可以在下一个sql命令中使用这个数组的值。你可以帮我这么做吗?
//connection
SqlConnection c = new SqlConnection("Data Source=localhost\\sqlexpress;Initial Catalog=BookStoreDataBase1;Integrated Security=True;Pooling=False;");
c.Open();
//first statment
string raf = string.Format("Select Id from [Order] WHERE customerID={0}", k);
SqlCommand comm1 = new SqlCommand(raf, c);
SqlDataReader r33 = comm1.ExecuteReader();
r33.Read();
int [] k2 = r3.GetInt32(0);
r33.Close();
//second statment
string raf4 = string.Format("Select * from orderDetails WHERE orderId={0}", k2);
SqlCommand comm14 = new SqlCommand(raf4, c);
SqlDataReader r333 = comm14.ExecuteReader();
答案 0 :(得分:0)
不太确定你问的是什么,但据我所知,你的第一个查询将返回包含不同ID的多行,并且你想在第二个命令中使用它们。这样做的基本方法是:
string IDs = "(";
while (r33.read())
{
IDs += r33.GetString(0) + ",";
}
IDs += ")"
IDs = IDs.Replace(",)",")");
然后使用IN关键字为您提供多个ID
string raf4 = string.Format("Select * from orderDetails WHERE orderId IN {0}", IDs);