SqlConnection conn = new SqlConnection("server =(local);database = shujuku;uid = sa;pwd = 123456");
conn.Open();
string tianjia1 = "insert into list(学号,姓名,专业,性别,民族,导师) values('" + textBox13 + "','" + textBox14 + "','" + textBox15 + "','" + textBox16 + "','" + textBox17 + "','" + textBox18 + "',)";
SqlCommand command = new SqlCommand(tianjia1, conn);
command.ExecuteNonQuery();
MessageBox.Show("数据添加成功");
以上是整体代码 运行后就是有这么一个错误 "','附近有语法错误,请高手指教 万分感谢
conn.Open();
string tianjia1 = "insert into list(学号,姓名,专业,性别,民族,导师) values('" + textBox13 + "','" + textBox14 + "','" + textBox15 + "','" + textBox16 + "','" + textBox17 + "','" + textBox18 + "',)";
SqlCommand command = new SqlCommand(tianjia1, conn);
command.ExecuteNonQuery();
MessageBox.Show("数据添加成功");
以上是整体代码 运行后就是有这么一个错误 "','附近有语法错误,请高手指教 万分感谢
这个逗号
conn.Open();
//string xuehao = textBox13.Text, xingming = textBox14.Text, zhuanye = textBox15.Text, xingbie = textBox16.Text, minzu = textBox17.Text, daoshi = textBox18.Text;
string tianjia1 = "insert into list(学号,姓名,专业,性别,民族,导师) values('" + textBox13 + "','" + textBox14 + "','" + textBox15 + "','" + textBox16 + "','" + textBox17 + "','" + textBox18 + "');
SqlCommand command = new SqlCommand(tianjia1, conn);
command.ExecuteNonQuery();
MessageBox.Show("数据添加成功");我把逗号去了 又有新问题了 就是一直显示两个错误 1:请输入;,2:常量中有换行符。请高手指教 谢谢
直接写名字貌似不可以的吧。
如果插入成功,用
Response.Write("<script>alert('数据添加成功!');</"+"script>");
替换你的messagebox那一行~
conn.Open();
string tianjia1 = "insert into list(学号,姓名,专业,性别,民族,导师) values('" + textBox13 + "','" + textBox14 + "','" + textBox15 + "','" + textBox16 + "','" + textBox17 + "','" + textBox18 + "',)";
SqlCommand command = new SqlCommand(tianjia1, conn);
command.ExecuteNonQuery();
MessageBox.Show("数据添加成功");逗号问题,以后做的话把SQL语句带到查询分析器里面执行一下
string tianjia1 = "insert into list(学号,姓名,专业,性别,民族,导师) values('" + textBox13 + "','" + textBox14 + "','" + textBox15 + "','" + textBox16 + "','" + textBox17 + "','" + textBox18 + "')";这种SQL语法容易被SQL注入式攻击
用参数如
string tianjial="INSERT INTO list(学号) VALUES(@STU)";
最后多了个“,”
"insert into list(学号,姓名,专业,性别,民族,导师) values('" + textBox13 + "','" + textBox14 + "','" + textBox15 + "','" + textBox16 + "','" + textBox17 + "','" + textBox18 + "')";
其实,这个很多字段的添加,用SqlParameter比较好,不会出现这种问题。
string sql="insert into list(学号,姓名,专业,性别,民族,导师) values(@txt1,@txt2,@txt3,@txt4,@txt5,@txt6)";SqlParameter[] para = new SqlParameter[]{
new SqlParameter("@FRowID",MDetail.FRowID),
new SqlParameter("@FMaterialID",MDetail.FMaterialID),
new SqlParameter("@FShelfID",MDetail.FShelfID),
new SqlParameter("@FQty",MDetail.FQty),
new SqlParameter("@FRe",MDetail.FRe),
new SqlParameter("@MPeriod",MIn.FPeriod)};SqlCommand cmd = new SqlCommand(sql, Connection);
cmd.CommandType = CommandType.Text;
cmd.Parameters.AddRange(values);
cmd.ExecuteNonQuery();像上面这样写就可以了,还可以防止sql注入。
希望可以帮助到你
string sql="insert into list(学号,姓名,专业,性别,民族,导师) values(@txt1,@txt2,@txt3,@txt4,@txt5,@txt6)";SqlParameter[] para = new SqlParameter[]{
new SqlParameter("@txt1",txtbox1.Text),
new SqlParameter("@txt2",txtbox2.Text),
new SqlParameter("@txt3",txtbox3.Text),
new SqlParameter("@txt4",txtbox4.Text),
new SqlParameter("@txt5",txtbox5.Text),
new SqlParameter("@txt6",txtbox6.Text)};SqlCommand cmd = new SqlCommand(sql, Connection);
cmd.CommandType = CommandType.Text;
cmd.Parameters.AddRange(values);
cmd.ExecuteNonQuery();
看看tianjia1的值 放进sql里执行 看能不能成功