C# 更新按钮不工作
我正试图通过C#中的windows窗体按钮更新我的数据库。当我运行即将显示的代码时,它不会显示任何错误,但不会更改数据库或数据网格中的数据C# 更新按钮不工作,c#,.net,database,winforms,C#,.net,Database,Winforms,我正试图通过C#中的windows窗体按钮更新我的数据库。当我运行即将显示的代码时,它不会显示任何错误,但不会更改数据库或数据网格中的数据 private void update_Click(object sender, EventArgs e) { SqlConnection NewCon = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=E:\ASPNET\cd\cdcwk2\cddatabase.mdf;
private void update_Click(object sender, EventArgs e)
{
SqlConnection NewCon = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=E:\ASPNET\cd\cdcwk2\cddatabase.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True");
SqlCommand cmd = new SqlCommand("UPDATE cdstock set cd_title='" + cdtext.Text + "', artist='" + artisttext.Text + "',genre='" + genretext.Text + "', rating=" + ratingtext.Text + ", cd_discription='" + distext.Text + "' where cd_id =" + idtext.Text + ";", NewCon);
MessageBox.Show("UPDATE cdstock set cd_title='" + cdtext.Text + "', artist='" + artisttext.Text + "',genre='" + genretext.Text + "', rating='" + ratingtext.Text + "', cd_discription='" + distext.Text + "' where cd_id ='" + idtext.Text + "'");
NewCon.Open();
cmd.ExecuteNonQuery();
NewCon.Close();
MessageBox.Show("Record Has Now Been UPDATED!!");
}
您好,我找到了解决方案,现在效果很好,谢谢您抽出时间。解决方案如下
String NewCon = @"Data Source=.\SQLEXPRESS;AttachDbFilename=E:\ASPNET\cd\cdcwk2\cddatabase.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True";
SqlConnection con = new SqlConnection(NewCon);
SqlCommand cmd = new SqlCommand();
cmd.CommandText = "UPDATE cdstock set cd_title='" + cdtext.Text + "', artist='" + artisttext.Text + "',genre='" + genretext.Text + "', cd_discription='" + distext.Text + "', rating=" + ratingtext.Text + " where cd_id = " + dataGridView1.CurrentRow.Cells[0].Value;
cmd.Connection = con;
cmd.Connection.Open();
cmd.ExecuteNonQuery();
cmd.Connection.Close();
cddatabaseDataSet3.GetChanges();
this.cdstockTableAdapter.Fill(this.cddatabaseDataSet3.cdstock)
您是否尝试手动运行显示的查询?是否使用SQL Management studio或其他SQL客户端运行生成的命令?什么是
cmd.ExecuteNonQuery()代码>返回?请检查下一行受影响的行数,int result=cmd.ExecuteNonQuery();您可以运行SQL Profiler,并查看服务器上的命令,然后对其进行调试。请使用参数化查询。使用参数化查询!使用参数化查询!!-我们需要多久重复一次?
MessageBox.Show("Record Has Now Been UPDATED!!");