永发信息网

给数据库添加不了信息

答案:3  悬赏:40  手机版
解决时间 2021-05-11 18:04
  • 提问者网友:藍了天白赴美
  • 2021-05-11 06:40

string connstr = "Data Source=WWW-0FD4D77EDB7;Initial Catalog=Exercise;Integrated Security=True";
string t1 = textBox1.Text;
string t2 = textBox2.Text;
string t3 = textBox3.Text;
string t4 = textBox4.Text;
string t5 = textBox5.Text;
string t6 = textBox6.Text;
string t7 = textBox7.Text;

SqlConnection conn = new SqlConnection(connstr);
conn.Open();
string sqlinsertNumber= "insert into Number(题号) into values(t1) ";
string sqlinsertQuestion="insert into Question(题号,题干) values(t1,t2)";
string sqlinsertSelect = "insert into Select(题号,A,B,C,D) values(t1,t3,t4,t5,t6) ";
string sqlinserAnswer="insert into Answer(题号,答案) values(t1,t7)";
if(MessageBox.Show("您确定要添加吗?","亲爱的同学",MessageBoxButtons.YesNo,MessageBoxIcon.Asterisk)==DialogResult.Yes)
{
SqlCommand myCommand1 = new SqlCommand(sqlinsertNumber, conn);
SqlCommand myCommand2 = new SqlCommand(sqlinsertQuestion, conn);
SqlCommand myCommand3 = new SqlCommand(sqlinsertSelect, conn);
SqlCommand myCommand4 = new SqlCommand(sqlinserAnswer, conn);
conn.Close();
MessageBox.Show("ok");

}

这是一个按钮的单击事件

我写得应该没错,编译器没报错,但是输完了就没反应,数据库还是没更新~

最佳答案
  • 五星知识达人网友:爱难随人意
  • 2021-05-11 07:12

myCommand1.ExecuteNonQuery();


myCommand2.ExecuteNonQuery();


myCommand3.ExecuteNonQuery();


myCommand4.ExecuteNonQuery();


你没执行语句么

全部回答
  • 1楼网友:洎扰庸人
  • 2021-05-11 08:40
楼上正解....错误太低级了,LZ努力!!
  • 2楼网友:不甚了了
  • 2021-05-11 07:36

没有SQL的执行。

流程大致为:

SqlConnection conn = new SqlConnection(“数据库字符串");

SqlCommand comm = new SqlCommand(sql语句,conn); comm.Connection = conn; comm.ExecuteNonQuery();

conn.close();

所以 你的程序缺少comm.ExecuteNonQuery();

SqlCommand myCommand1 = new SqlCommand(sqlinsertNumber, conn);

myCommand1.ExecuteNonQuery(); SqlCommand myCommand2 = new SqlCommand(sqlinsertQuestion, conn);

myCommand2.ExecuteNonQuery(); SqlCommand myCommand3 = new SqlCommand(sqlinsertSelect, conn);

myCommand3.ExecuteNonQuery(); SqlCommand myCommand4 = new SqlCommand(sqlinserAnswer, conn);

myCommand4.ExecuteNonQuery();

我要举报
如以上回答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
点此我要举报以上问答信息
大家都在看
推荐资讯