在文本框中输入多个值
比如表中 id name两列
文本框中输入 1,2;2,4将这两行值插入到数据库中

解决方案 »

  1.   


            string text = "1,2;2,4";//textBox1.Text;
            string insert = "insert into table_T values(@val1, @val2)";
            SqlCommand command = new SqlCommand(insert);
            command.Parameters.Add("@val1", SqlDbType.Int);
            command.Parameters.Add("@val2", SqlDbType.Int);
            string[] rows = text.Split(';');
            foreach (string row in rows) {
                string[] values = row.Split(',');
                command.Parameters[0].Value = int.Parse(values[0]);
                command.Parameters[1].Value = int.Parse(values[1]);
                command.ExecuteNonQuery();
            }
      

  2.   

    string str1 = "1,2;3,4;5,6";
    string[] strA ;
    strA = str1.Split(';');
    for (int i = 0; i < strA.Length; i++)
    {
       string[] strB;
       strB = strA[i].Split(',');
       string insert = "insert into table values(strB[0],strB[1])";
       ......
    }
      

  3.   

    string str1 = "1,2;3,4;5,6";
    string[] strA ;
    strA = str1.Split(';');
    for (int i = 0; i < strA.Length; i++)
    {
      string[] strB;
      strB = strA[i].Split(',');
      string insert = "insert into table values('"+strB[0]+"','"+strB[1]+"')";
      ......
    }