数据使用选中的列表框插入数据库的回溯

时间:2014-02-07 07:48:46

标签: c#-4.0

如何使用c#中的checkedlistbox向/从数据库插入和检索数据 这是我的鳕鱼按钮点击事件:

string CS = ConfigurationManager.ConnectionStrings["RosterFinal"].ConnectionString;
        using (SqlConnection con = new SqlConnection(CS))
        {
            var Sep = "";
            string INSERTq = "insert into T_Entity values(@Tno,@Tname,@STime,@ETime,@Skill1,@S_Day1)";
            SqlCommand cmd = new SqlCommand(INSERTq, con);
            cmd.Parameters.AddWithValue("@Tno", txtTNo.Text);
            cmd.Parameters.AddWithValue("@Tname", txtTNa.Text);
            cmd.Parameters.AddWithValue("@STime", dateTimePicker1.Text);
            cmd.Parameters.AddWithValue("@ETime", dateTimePicker2.Text);
            foreach (object i in checkedListBox1.CheckedItems )
            {
               // cmd.Parameters.Clear();
                cmd.Parameters.AddWithValue("@Skill1", checkedListBox1.SelectedItem.ToString());                   
            }
            foreach (object m in cbDay.CheckedItems)
            {
               // cmd.Parameters.Clear();
                cmd.Parameters.AddWithValue("@S_Day1", cbDay.SelectedItem.ToString());
            }             
            con.Open();
            cmd.ExecuteNonQuery();
            cmd.Parameters.Clear();
        }

scaller参数并不是readind,并且scaller参数skill1必须是唯一的例外

2 个答案:

答案 0 :(得分:0)

此异常是因为为单个insert语句多次设置参数“@ Skill1”和“@S_Day1”。

另外,在foreach循环中,每次都设置相同的值,这是没用的。

我建议你使用以下代码,

using (SqlConnection con = new SqlConnection(CS))
{
        var Sep = "";
        string INSERTq = "insert into T_Entity values(@Tno,@Tname,@STime,@ETime,@Skill1,@S_Day1)";
        SqlCommand cmd = new SqlCommand(INSERTq, con);
        cmd.Parameters.AddWithValue("@Tno", txtTNo.Text);
        cmd.Parameters.AddWithValue("@Tname", txtTNa.Text);
        cmd.Parameters.AddWithValue("@STime", dateTimePicker1.Text);
        cmd.Parameters.AddWithValue("@ETime", dateTimePicker2.Text);
        cmd.Parameters.AddWithValue("@Skill1", checkedListBox1.SelectedItem.ToString());                   
        cmd.Parameters.AddWithValue("@S_Day1", cbDay.SelectedItem.ToString());
        con.Open();
        cmd.ExecuteNonQuery();
        cmd.Parameters.Clear();
    }

答案 1 :(得分:0)

我认为这可能有助于你..
比较这段代码并修改为urs ..

ConnectionStringSettings consettings = ConfigurationManager.ConnectionStrings["attendancemanagement"];
       string connectionString = consettings.ConnectionString;
       SqlConnection cn = new SqlConnection(connectionString);
       cn.Open();
string query = "select employee_id,Employee_name from employee_details order by employee_name";
           SqlCommand cmd = new SqlCommand(query, cn); 
           SqlDataAdapter sda ;
           DataSet ds = new DataSet();
           sda = new SqlDataAdapter(cmd);
           sda.Fill(ds);
           DataTable dt = ds.Tables[0];
           foreach (DataRow datarow in dt.Rows)
           {
               checkedListBox1.Items.Add(datarow["employee_id"] + ":    " + datarow["Employee_name"]);

           }