中易网

C#无法写入数据库数据

答案:4  悬赏:80  
解决时间 2021-04-28 17:16
  • 提问者网友:他是我的あ男人
  • 2021-04-27 18:26
private void button1_Click(object sender, EventArgs e)
{
if (textBox1.Text == "" || textBox2.Text == "" || textBox3.Text == "")
{
MessageBox.Show("输入信息不完整,重新输入!", "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
else
{
SqlConnection con = new SqlConnection("Data Source=20090111-1011\\SQLEXPRESS;AttachDbFilename=|DataDirectory|\\yh.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True");
StringBuilder strSQL = new StringBuilder();
strSQL.Append("insert into zc_info(yhm,mm)");
strSQL.Append("values('" + textBox1.Text.Trim().ToString() + "','" + textBox2.Text.Trim().ToString() + "')");
using (SqlCommand cmd = new SqlCommand(strSQL.ToString(), con))
{
con.Open();
cmd.ExecuteNonQuery();
con.Close();
con.Dispose();
}
MessageBox.Show("注册成功!", "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
textBox1.Text = "";
textBox2.Text = "";
textBox3.Text = "";

this.Hide();
Form2 frm2 = new Form2();
frm2.Show();
}
输入数据后无提示错误,但是SQL 数据库中没有数据 帮忙解决下 谢谢了!!

测试时显示连接成功,但是在运行时数据库图标下方有个叉 一楼的方法我试过了但是还是写入不了数据

我试了四楼的代码有错误

数据库没有数据 但是登陆却可以
最佳答案
  • 二级知识专家网友:我的任性你不懂
  • 2021-04-27 19:36
1先请确认数据库连接是否正确
2strSQL.Append("insert into zc_info(yhm,mm)");
strSQL.Append("values('" + textBox1.Text.Trim().ToString() + "','" + textBox2.Text.Trim().ToString() + "')");
Sql有问题values前要有空格的
全部回答
  • 1楼网友:哭不代表软弱
  • 2021-04-27 22:37
添加需要在values前面加空格 strSQL.Append("insert into zc_info(yhm,mm) "); strSQL.Append("values('" + textBox1.Text.Trim().ToString() + "','" + textBox2.Text.Trim().ToString() + "')");
  • 2楼网友:糜废丧逼
  • 2021-04-27 21:45
using system.data.oledb; string sql = "insert into 表 (列1,列2,列3) values('"+combobox1.text+"','"+combobox1.text+"','"+textbox1.text+"')"; //构造sql语句 string dbpath = appdomain.currentdomain.basedirectory + "\\data\\database1.mdb";//access数据库的路径 oledbconnection con = new oledbconnection("provider=microsoft.jet.oledb.4.0; data source=" + dbpath); //定义数据库连接对象 oledbcommand cmd = new oledbcommand(sql, con); //定义command对象 con.open(); //打开数据库连接 cmd.executenonquery(); //执行command命令 con.close(); //关闭数据库连接 有问题可以追问。
  • 3楼网友:闲懒诗人
  • 2021-04-27 20:17
//添加注册 private void button2_Click(object sender, EventArgs e) { if (textBox1.Text.Length != 0 && textBox2.Text.Length != 0) { SqlConnection sc = new SqlConnection("data source=.;initial catalog=Cexercise;user id=sa;password=123;"); try { sc.Open(); SqlCommand scd = new SqlCommand("select * from userss where name='" + textBox1.Text.Trim() + "'", sc); SqlDataReader sdr = scd.ExecuteReader(); if (!sdr.HasRows) { sdr.Close(); SqlCommand scon = new SqlCommand("insert into userss values('" + textBox1.Text + "','" + textBox2.Text + "')", sc); int res = scon.ExecuteNonQuery(); MessageBox.Show("注册成功," + textBox1.Text + "欢迎你!"); } else { MessageBox.Show("注册失败,已经有该用户,请重新输入注册信息^ _^"); textBox1.Text = ""; textBox2.Text = ""; textBox1.Focus(); } } catch (Exception ex) { MessageBox.Show("操作失败,请检查网络连接是否正常!"); } finally { if (sc.State == ConnectionState.Open) sc.Close(); } } }
我要举报
如以上回答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
点此我要举报以上问答信息!
大家都在看
推荐信息