C# winform 数据库中无法插入数据?语句没错,请高手指点 10
代码没有提示错误,但是数据库中没有插入任何数据,这是怎么回事?代码如下:stringconnstr=@"DataSource=.\SQLEXPRESS;AttachDbF...
代码没有提示错误,但是数据库中没有插入任何数据,这是怎么回事?
代码如下:
string connstr = @"Data
Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\Database2.mdf;Integrated Security=True;User Instance=True";
private void button1_Click(object sender, EventArgs e)
{
SqlConnection conn = new SqlConnection(connstr);
string sql = "insert into Table1(x,y) values('" + textBox1.Text.Trim() +"','" + textBox2.Text.Trim() + "')";
SqlCommand cmd = new SqlCommand(sql, conn);
conn.Close();
try {
conn.Open(); MessageBox.Show("成功");
}
catch { MessageBox.Show("失败"); }
cmd.ExecuteNonQuery();
conn.Close();
label1.Text = "插入成功";
} 展开
代码如下:
string connstr = @"Data
Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\Database2.mdf;Integrated Security=True;User Instance=True";
private void button1_Click(object sender, EventArgs e)
{
SqlConnection conn = new SqlConnection(connstr);
string sql = "insert into Table1(x,y) values('" + textBox1.Text.Trim() +"','" + textBox2.Text.Trim() + "')";
SqlCommand cmd = new SqlCommand(sql, conn);
conn.Close();
try {
conn.Open(); MessageBox.Show("成功");
}
catch { MessageBox.Show("失败"); }
cmd.ExecuteNonQuery();
conn.Close();
label1.Text = "插入成功";
} 展开
1个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询