查询数据库并输出结果以在C#中构建IN语句
Query a database and output result to build IN statement in C#
我在 C# 中有一个查询,我正在从我的服务器上执行该查询。它 returns 一列,但我想获取该列的结果并将它们生成到 IN 语句中,以传递给不同服务器的另一个查询。
这是代码,我只是不知道如何编写输出每一行的逻辑:
System.Data.DataTable dtCups = new System.Data.DataTable();
var dataSet = new DataSet();
string connString = "Data Source=My Server;" + "Integrated Security=SSPI;";
SqlConnection Conn = new SqlConnection(connString);
Conn.Open();
String qryCups = "select cups from table"; //returns 300 rows
SqlDataAdapter adapter = new SqlDataAdapter(qryCups, Conn);
adapter.Fill(dtCups);
Conn.Close();
foreach (DataRow dr in dtCups.Rows)
{
string cups = dr.ItemArray[0].ToString();
DataRow[] MatchRecs = dtCups.Select("cups='" + dr.ItemArray[0] + "'");
}
只是不确定要做什么,因为它循环遍历每条记录,但没有将先前的杯子值附加到下一个值,例如 'cup1'、'cup2' 等
提前致谢。
下一个 sql 语句的代码将是
String qryFinal = "select cups from table_B where cups in '" + [the output of the data above] + "'";
然后我会将结果插入最终 table 或下降到 Excel(我知道该怎么做);
您应该阅读 table 并逐行创建两个列表。第一个将包含参数名称,第二个将包含参数本身以及数据提取的值table。
在循环出口处,您构建一个查询文本,将 IN 语句中的参数名称连接在一起,然后将参数传递给 SqlCommand 本身。
List<string> pNames = new List<string>();
List<SqlParameter> prms = new List<SqlParameter>();
for (int x = 0; x < dtCups.Rows.Count; x++)
{
// Create the parameters names list @0, @1, @2 etc..
pNames.Add($"@{x}");
// Create the parameters, note that I assume a string type parameter with a length of 255 characters max. Adjust if assumption is wrong
SqlParameter p = new SqlParameter($"@{x}", SqlDbType.NVarChar, 255);
p.Value = dtCups.Rows[x][0].ToString();
prms.Add(p);
}
.....
// Create the final sql command text from the parameters names
string qryFinal = "select cups from table_B where cups in(" +
string.Join(",", pNames) + ")";
SqlCommand cmd = new SqlCommand(qryFinal, connection);
// Add the parameter list to the command collection and then execute
cmd.Parameters.AddRange(prms.ToArray());
SqlDataReader reader = cmd.ExecuteReader()
// Or DataAdapter.Fill table
DataTable aTable = new DataTable();
SqlDataAdapter da1 = new SqlDataAdapter(cmd);
da1.Fille(aTable);
我在 C# 中有一个查询,我正在从我的服务器上执行该查询。它 returns 一列,但我想获取该列的结果并将它们生成到 IN 语句中,以传递给不同服务器的另一个查询。
这是代码,我只是不知道如何编写输出每一行的逻辑:
System.Data.DataTable dtCups = new System.Data.DataTable();
var dataSet = new DataSet();
string connString = "Data Source=My Server;" + "Integrated Security=SSPI;";
SqlConnection Conn = new SqlConnection(connString);
Conn.Open();
String qryCups = "select cups from table"; //returns 300 rows
SqlDataAdapter adapter = new SqlDataAdapter(qryCups, Conn);
adapter.Fill(dtCups);
Conn.Close();
foreach (DataRow dr in dtCups.Rows)
{
string cups = dr.ItemArray[0].ToString();
DataRow[] MatchRecs = dtCups.Select("cups='" + dr.ItemArray[0] + "'");
}
只是不确定要做什么,因为它循环遍历每条记录,但没有将先前的杯子值附加到下一个值,例如 'cup1'、'cup2' 等
提前致谢。
下一个 sql 语句的代码将是
String qryFinal = "select cups from table_B where cups in '" + [the output of the data above] + "'";
然后我会将结果插入最终 table 或下降到 Excel(我知道该怎么做);
您应该阅读 table 并逐行创建两个列表。第一个将包含参数名称,第二个将包含参数本身以及数据提取的值table。 在循环出口处,您构建一个查询文本,将 IN 语句中的参数名称连接在一起,然后将参数传递给 SqlCommand 本身。
List<string> pNames = new List<string>();
List<SqlParameter> prms = new List<SqlParameter>();
for (int x = 0; x < dtCups.Rows.Count; x++)
{
// Create the parameters names list @0, @1, @2 etc..
pNames.Add($"@{x}");
// Create the parameters, note that I assume a string type parameter with a length of 255 characters max. Adjust if assumption is wrong
SqlParameter p = new SqlParameter($"@{x}", SqlDbType.NVarChar, 255);
p.Value = dtCups.Rows[x][0].ToString();
prms.Add(p);
}
.....
// Create the final sql command text from the parameters names
string qryFinal = "select cups from table_B where cups in(" +
string.Join(",", pNames) + ")";
SqlCommand cmd = new SqlCommand(qryFinal, connection);
// Add the parameter list to the command collection and then execute
cmd.Parameters.AddRange(prms.ToArray());
SqlDataReader reader = cmd.ExecuteReader()
// Or DataAdapter.Fill table
DataTable aTable = new DataTable();
SqlDataAdapter da1 = new SqlDataAdapter(cmd);
da1.Fille(aTable);