我在不同的SQL Server数据库中有两个表。
我想显示所有购买的产品通过特定的客户ID。
我的代码:
using System.Data.SqlClient;
string queryString = "Select custID from Table1 where custId ="+ textbox1.text;
string TempCustID;
using (SqlConnection connection = new SqlConnection(connectionString))
{
SqlCommand command = new SqlCommand(queryString, connection);
connection.Open();
SqlDataReader reader = command.ExecuteReader();
while (reader.Read())
{
if (!reader.IsDBNull(0))
{
TempCustID = reader[0].ToString();
String stringprovider = "@database connection string ";
Sqlconnection con2 = new sqlconnection(stringprovider);
Con2.open();
Cmd2.connection=con2;
Cmd2.commandType = new commandType.text;
Cmd2.commandText = "select * from Table2 where Productid = @productid";
Cmd2.parameters.addwithvalue("@productid",TempCustID);
}
}
reader.Close();
Dataset Ds = new dataset();
Oledbdataadaptaer da1 = new oledbdataadapter(cmd2);
Datatable Table2 = new Data table();
Da1.fill(table2);
Datagridview2.source = table2;
}
在此,我仅获得该客户的第一个产品详细信息,而不是一次显示所有产品。
将此分为两种方法。首先将根据条件从数据库中获取客户ID。然后将这些客户ID传递给第二种方法并获取产品详细信息。您可以从第一种方法创建客户ID列表,并在第二种方法中构建SQL IN子句。从一行的字符串列表中引用构建SQL“ where in”语句?
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句