'mainMenuNum'附近的语法不正确

艾哈迈德

在这里,我得到了动态创建的文本框的值

protected void Submit(object sender, EventArgs e)
{
    var textboxValues = new List<string>();
    var textboxValues1 = new List<string>();

    if (Request.Form.HasKeys())
    {
        Request.Form.AllKeys.Where(i =>  i.Contains("Textbox")).ToList().ForEach(i => { textboxValues.Add(Request.Form[i]); });
        Request.Form.AllKeys.Where(j => j.Contains("Textbox1")).ToList().ForEach(j => { textboxValues1.Add(Request.Form[j]); });
    }

    SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["IvrContext"].ConnectionString);
    conn.Open();

列数据类型为nvarchar我不知道这是什么问题。请帮忙!

    SqlCommand com = new SqlCommand("INSERT INTO menudatas [mainMenuNum] values (@mainMenuNum)", conn);

    for(int x = 0; x<textboxValues.Count(); x++)
    { 
        com.Parameters.AddWithValue("@mainMenuNum", textboxValues[x]);
        com.ExecuteNonQuery();
    }

    SqlCommand com1 = new SqlCommand("INSERT INTO menudatas [mainMenuText] values (@mainMenuText)" , conn);

    for (int y = 0; y < textboxValues1.Count(); y++)
    {  
        com.Parameters.AddWithValue("@mainMenuText" , textboxValues1[y]);
        com1.ExecuteNonQuery();
        container.Visible = false;
    }

    conn.Close();
}
marc_s

好吧,您一直在向中添加一个参数-每次都具有相同的名称SqlCommand您需要一次定义参数在循环之前-然后在循环中为其分配

SqlCommand com = new SqlCommand("INSERT INTO menudatas (mainMenuNum) values (@mainMenuNum)", conn);

// define your parameter ONCE before the loop
com.Parameters.Add("@mainMenuNum", SqlDbType.VarChar, 20);   // adapt the datatype to match your situation

for(int x = 0; x<textboxValues.Count(); x++)
{ 
    // only assign the VALUE to the parameter repeatedly, in the loop
    com.Parameters["@mainMenuNum"].Value = textboxValues[x]);
    com.ExecuteNonQuery();
}

SqlCommand com1 = new SqlCommand("INSERT INTO menudatas (mainMenuText) values (@mainMenuText)" , conn);

// define your parameter ONCE before the loop - and add it to the **correct** SqlCommand "com1" - not "com" ....
com1.Parameters.Add("@mainMenuText", SqlDbType.VarChar, 100);  // adapt the datatype to match your situation

for (int y = 0; y < textboxValues1.Count(); y++)
{  
    // only assign the VALUE to the parameter repeatedly, in the loop
    com.Parameters["@mainMenuText"].Value = textboxValues1[y]);
    com1.ExecuteNonQuery();
    container.Visible = false;
}

conn.Close();

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章