我的2.txt文件里的内容如下11 qqq tr yu iu op每个字符串之间用空格分开的,我想把每个字符插入到数据表中,就是11插入到数据表d1字段,qqq插入到数据表d2字段,...,op插入到数据表d6字段,我现在不知道怎么能写进数据库,标红色的地方不会,请大家指点一下,谢谢!private void button1_Click(object sender, EventArgs e)
        {
            using(StreamReader sr=File.OpenText(@"F:\2.txt"))
          {
              string input = null;
              input = sr.ReadLine().ToString().Replace(' ', ',');
              string[] str = hh.Split(',');
              ArrayList mylist = new ArrayList();
              foreach (string ll in str)
              {
                  mylist.Add(ll);
                  for (int i = 0; i <mylist.Count; i++)
                  {
                      label1.Text = mylist[i].ToString();
                      SqlConnection SqlCon = new SqlConnection("server=192.168.0.23;database=tb;uid=test;pwd=test;");
                      string StrSql = "insert into DataTable(id,d1,d2,d3,d4,d5,d6) values ()";
                      SqlCommand SqlCom = new SqlCommand(StrSql,SqlCon);
                      SqlCon.Open();
                      SqlCom.ExecuteNonQuery();
                  }
              }
                
          }
        }

解决方案 »

  1.   

    using(StreamReader sr=File.OpenText(@"F:\2.txt"))
      {
      string input = null;
      input = sr.ReadLine().ToString().Trim();
      SqlConnection SqlCon = new SqlConnection("server=192.168.0.23;database=tb;uid=test;pwd=test;");
      string StrSql = "insert into DataTable(id,d1,d2,d3,d4,d5,d6) values ('" + input.Replace(" ", "','" + "')";
      SqlCommand SqlCom = new SqlCommand(StrSql,SqlCon);
      SqlCon.Open();
      SqlCom.ExecuteNonQuery();
      }
      }
      

  2.   

    string input = null;
    input = sr.ReadLine().ToString().Replace(' ', ',');
    string[] str = hh.Split(',');
    SqlConnection SqlCon = new SqlConnection("server=192.168.0.23;database=tb;uid=test;pwd=test;");
    string StrSql = "insert into DataTable(id,d1,d2,d3,d4,d5,d6) values (0,'"+str[0]+"','"+str[1]+"','"+str[2]+"','"+str[3]+"','"+str[4]+"','"+str[5]+"')";
    SqlCommand SqlCom = new SqlCommand(StrSql,SqlCon);
    SqlCon.Open();
    SqlCom.ExecuteNonQuery();
      

  3.   

    foreach(string s in List<String)(File.ReadAllLines("")))
    {
      s=s.Replace(" ","','");
    }
    或string[] arr=s.Split(' ');