且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何将列表框中的选定项目添加到数据库中每个选定项目到新记录

更新时间:2023-10-08 11:31:10

希望这符合您的要求



Hope this will meet your requirement

foreach (ListItem lst in test_names.Items)
       {
           if (lst.Selected)
           {
               string selectedValue = lst.Value;
               string patient_id = Request.QueryString["id"];
            SqlConnection cnm = new SqlConnection(ConfigurationManager.ConnectionStrings["lap_connection"].ConnectionString);
            SqlCommand cmd = new SqlCommand("INSERT INTO [tetstttt] (test_id,date,pname) VALUES ('" + selectedValue + "','" + DateTime.Now.ToString("yyyy-MM-dd") + "','" + patient_id + "');", cnm);
            cmd.Connection = cnm;
            cmd.CommandType = CommandType.Text;
            cnm.Open();
            cmd.ExecuteScalar();
            cnm.Close();
            ScriptManager.RegisterStartupScript(this, this.GetType(), "show_error", "showSuccess('Test Has Been Added Successfully',2000);", true);
              
} }</pre>


试试这个





Try this


for (int indexCounter = 0; indexCounter < test_names.Items.Count; indexCounter++)
       {
           if (test_names.Items[indexCounter].Selected)
           {

               string patient_id = Request.QueryString["id"];
               SqlConnection cnm = new SqlConnection(ConfigurationManager.ConnectionStrings["lap_connection"].ConnectionString);
               SqlCommand cmd = new SqlCommand("INSERT INTO [tetstttt] (test_id,date,pname) VALUES ('" + test_names.Items[indexCounter] + "',N'" + DateTime.Now.ToString("yyyy-MM-dd") + "','" + patient_id + "');", cnm);
               cmd.Connection = cnm;
               cmd.CommandType = CommandType.Text;
               cnm.Open();
               cmd.ExecuteScalar();
               cnm.Close();
               ScriptManager.RegisterStartupScript(this, this.GetType(), "show_error", "showSuccess('Test Has Been Added Successfully',2000);", true);
           }
       }


你好



另一个我试图添加更多感谢信息,但我收到错误



hello

another thing iam trying to add more thank one filed but i get error

foreach (ListItem lst in test_names.Items)
        {
            if (lst.Selected)
            {
                string selectedValue = lst.Value;
                string patient_id = Request.QueryString["id"];
                SqlConnection cnm = new SqlConnection(ConfigurationManager.ConnectionStrings["lap_connection"].ConnectionString);
                SqlCommand cmd = new SqlCommand("INSERT INTO [patient_test_data] (test_id,test_date,pat_id,data_id) VALUES ('" + selectedValue + "','" + DateTime.Now.ToString("yyyy-MM-dd") + "','" + patient_id + "');", cnm);
                cmd.Connection = cnm;
                cmd.CommandType = CommandType.Text;
                cnm.Open();
                cmd.ExecuteScalar();
                cnm.Close();
                ScriptManager.RegisterStartupScript(this, this.GetType(), "show_error", "showSuccess('Test Has Been Added Successfully',2000);", true);

            }
        }







这是错误我得到了



INSERT语句中的列数多于VALUES子句中指定的值。 VALUES子句中的值的数量必须与INSERT语句中指定的列数相匹配。





i不知道如何插入此功能




this is the error that i got

There are more columns in the INSERT statement than values specified in the VALUES clause. The number of values in the VALUES clause must match the number of columns specified in the INSERT statement.


i don't know how to insert with this function