C# 从MySQL更新数据
您好,我创建了一个程序,可以添加/编辑/删除数据库中的数据 我已经完成了添加数据的过程,但我的问题是我无法编辑和删除数据。 无论何时单击“编辑”按钮,都会显示错误消息 以下是错误消息: “对于不返回任何键列信息的SelectedCommand,不支持为UpdateCommand生成动态SQL” 这是我编辑按钮的代码C# 从MySQL更新数据,c#,mysql,C#,Mysql,您好,我创建了一个程序,可以添加/编辑/删除数据库中的数据 我已经完成了添加数据的过程,但我的问题是我无法编辑和删除数据。 无论何时单击“编辑”按钮,都会显示错误消息 以下是错误消息: “对于不返回任何键列信息的SelectedCommand,不支持为UpdateCommand生成动态SQL” 这是我编辑按钮的代码 try { string contactNumVal = txtCcontact.Text;
try
{
string contactNumVal = txtCcontact.Text;
if (contactNumVal.Length < 11)
{
MessageBox.Show("The Contact Number must have 11 digit");
}
else
{
DialogResult dw;
dw = MessageBox.Show("Are you sure you want to Edit this data", "Confirm Deletion", MessageBoxButtons.YesNo);
if (dw == DialogResult.Yes)
{
string MyConString = "SERVER=localhost;" +
"DATABASE=prototype_db;" +
"UID=root;";
MySqlConnection connection = new MySqlConnection(MyConString);
connection.Open();
MySqlCommand command = connection.CreateCommand();
DataTable dt = new DataTable();
MySqlDataAdapter da = new MySqlDataAdapter("Select ClientName,ClientAddress,ClientContactNo,ClientContactPerson from client_profile where ClientName like '%"+txtCname.Text+"%'" +" ",connection);
da.Fill(dt);
dt.Rows[0].BeginEdit();
dt.Rows[0][0] = txtCname.Text;
dt.Rows[0][1] = txtCaddress.Text;
dt.Rows[0][2] = txtCcontact.Text;
dt.Rows[0][3] = txtCconPer.Text;
dt.Rows[0].EndEdit();
MySqlCommandBuilder cb = new MySqlCommandBuilder(da);
da.Update(dt);
connection.Close();
MessageBox.Show("Data is now updated");
}
}
}
catch (Exception error)
{
MessageBox.Show(error.ToString());
}
试试看
{
字符串contactNumVal=txtCcontact.Text;
如果(触点numval.Length<11)
{
MessageBox.Show(“联系人号码必须有11位”);
}
其他的
{
结果dw;
dw=MessageBox.Show(“是否确实要编辑此数据”,“确认删除”,MessageBox按钮.YesNo);
如果(dw==DialogResult.Yes)
{
字符串MyConString=“服务器=本地主机;”+
“数据库=原型数据库;”+
“UID=root;”;
MySqlConnection=newmysqlconnection(MyConString);
connection.Open();
MySqlCommand=connection.CreateCommand();
DataTable dt=新的DataTable();
MySqlDataAdapter da=new MySqlDataAdapter(“从客户端配置文件中选择ClientName、ClientAddress、ClientContactNo、ClientContactPerson,其中ClientName类似“%”+txtCname.Text++“%”,connection);
da.填充(dt);
dt.Rows[0].BeginEdit();
dt.Rows[0][0]=txtCname.Text;
dt.Rows[0][1]=txtcadress.Text;
dt.Rows[0][2]=txtCcontact.Text;
dt.Rows[0][3]=txtCconPer.Text;
dt.Rows[0].EndEdit();
MySqlCommandBuilder cb=新的MySqlCommandBuilder(da);
数据更新(dt);
connection.Close();
MessageBox.Show(“数据现在已更新”);
}
}
}
捕获(异常错误)
{
Show(error.ToString());
}
感谢您的回答如果您希望表的主键能够执行更新和删除(选择和插入没有问题),那么DataAdapter需要获取表的主键信息,但是您的查询不会返回此信息,因为您的命令文本中没有包含客户端ID字段 因此,您应该将代码更改为
using(MySqlConnection connection = new MySqlConnection(MyConString))
{
connection.Open();
DataTable dt = new DataTable();
using(MySqlDataAdapter da = new MySqlDataAdapter("Select Client_ID, ClientName, " +
"ClientAddress, ClientContactNo,ClientContactPerson " +
"from client_profile where ClientName like @cname",connection))
{
da.SelectCommand.Parameters.AddWithValue("@cname", "%" + txtCname.Text +"%");
da.Fill(dt);
}
}
还要注意,我已经删除了WHERE条件中使用的ClientName值的字符串连接。在生成sql命令时,始终使用参数,而不是用户直接键入的文本。这样可以避免SQL注入问题和解析问题(txtCName文本框中的单引号会破坏字符串连接)
如上所述添加客户机ID后,还应更改用于更新行值的索引
dt.Rows[0][1] = txtCname.Text;
dt.Rows[0][2] = txtCaddress.Text;
dt.Rows[0][3] = txtCcontact.Text;
dt.Rows[0][4] = txtCconPer.Text;
您的表是否有主键?我强烈建议您使用
SqlCommand、SqlConnection,并将该查询更改为使用参数化查询
尝试将Client\u ID
添加到SELECT
语句注意,Client\u ID是DataRow[0]的ItemArray中的第一个元素,你不应该改变它。