使用c#在存储过程中添加参数

时间:2013-11-15 09:47:16

标签: c# stored-procedures

我想根据控件号的输入自动添加参数。

以下代码将为我提供sp_INSERT @ COL5,sp_INSERT @ COL4,等等......

control = 5;

while(1<=control)
column = '@COL'
string setValues = "sp_INSERT'" + column + control + "';"

control = control - 1;

我想要实现的是sp_INSERT @ COL5,@ COL4,@ COL3,等......

2 个答案:

答案 0 :(得分:0)

刚刚......循环?

int control = 5;
string colPrefix = "@COL";
var sql = new StringBuilder("sp_INSERT ").Append(colPrefix).Append(control);
// note first item has different format due to comma
for(int i = control - 1; i > 0; i--)
{
    sql.Append(", ").Append(colPrefix).Append(i);
}
sql.Append(';');
string s = sql.ToString();

答案 1 :(得分:0)

简单循环,不是一个完整的解决方案,但它可能会有所帮助......

string myString = "INSERT ";
for (int i = 5; i > 0; i--)
    myString = string.Format("{0} @COL{1}, ", myString, i);
相关问题