各位大侠,请看下面代码
   string strTxt;
   SqlConnection conn = new SqlConnection("Data   Source=localhost;DataBase=researchdb;user=sa;password=gh001");
conn.Open();
SqlDataAdapter da = new  SqlDataAdapter("select * from sorttable",conn);
strTxt=SortNameTB.Text ;
string mySelectQuery="insert into sorttable values('004',strTxt)";
SqlCommand myCommand = new SqlCommand(mySelectQuery,conn);
myCommand.ExecuteNonQuery(); 
conn.Close ();
奇怪,出错!!!!!!!真不解why!
高分相送.
急!!!!!在线盼!!!!!!!!!!!!!

解决方案 »

  1.   

    string mySelectQuery="insert into sorttable values('004',strTxt)";
    改称
    string mySelectQuery="insert into sorttable values('004'," + strTxt + ")";
      

  2.   

    sorry!string mySelectQuery="insert into sorttable values('004',strTxt)";
    改称
    string mySelectQuery="insert into sorttable values('004','" + strTxt + "')";
      

  3.   

    string mySelectQuery="insert into sorttable values('004','"+strTxt+"')";
      

  4.   

    string mySelectQuery="insert into sorttable values('004',strTxt)";有问题
    strTxt是你定义的变量,
    改成:
    string mySelectQuery="insert into sorttable values('004','" + strTxt + "')";
      

  5.   

    string mySelectQuery="insert into sorttable values('004',strTxt)";
    改为
    string mySelectQuery="insert into sorttable values('004',"+strTxt+")";
      

  6.   

    to  sunjian_qi(sonne)
    我在文本框中输入"这次一定行",结果出现如下错误:
    在此上下文中不允许使用 '这次一定行'。此处只允许使用常量、表达式或变量。不允许使用列名。
      

  7.   

    to  sunjian_qi(sonne),好人啊,大哥,太感谢了!
      

  8.   

    sorttable的第二个字段是char型的吧,用
    string mySelectQuery="insert into sorttable values('004','" + strTxt + "')";
    这个行
      

  9.   

    SQL语句中有变量时,一定要将变量用'括起来.
    比如:
    string val = "hhhhhh";
    "SET field1 = '"+
    val+
    "'";