我无法更新我的mysql数据库 [英] i cant update my mysql database
本文介绍了我无法更新我的mysql数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有两种形式,主要形式和形式更新.表单更新包含一些文本框和按钮,主表单包含来自数据库的datagridview.我想使用表单更新来更新数据库.我尝试过此代码,但无法更新..
i have two form, main form and form update. form update contain is some textboxes and button, and main form containt datagridview from database.. i want to update the database using form update. i tried this code, but it can''t be update..
private void button1_Click_1(object sender, EventArgs e)
{
try
{
Form1 form1 = (Form1)this.Owner;
MySqlConnection conn = new MySqlConnection("server=localhost;User Id=root;database=sma9");
dt = new DataTable();
MySqlDataAdapter adp = new MySqlDataAdapter("UPDATE tbukux SET judulbuku="+txtjudul.Text+",namakategori=" + txtkategori.Text + ",pengarang=" + txtpengarang.Text + ",penerbit" + txtpenerbit.Text + ",tahunterbit=" + txttahun.Text + ",stokbuku=" + txtstok.Text + ",tglpenerimaan=" + tglter.Text + ") WHERE kodebuku=" + txtkode.Text + "", conn);
adp.Fill(dt);
MessageBox.Show("Data Berhasil Disimpan");
DataTable dt1 = new DataTable();
MySqlDataAdapter adp1 = new MySqlDataAdapter("select * from tbukux", conn);
adp1.Fill(dt1);
form1.gridbuku.DataSource = dt1;
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
MySql.Data.mysqlclient.mysqlexception(0x800040005):您的sql语法出错blah blah
请从上面的代码中帮助我
MySql.Data.mysqlclient.mysqlexception (0x800040005) ": you have en error in your sql syntax blah blah blah
please help me
推荐答案
我认为您应该在UPDATE命令文本中的WHERE之前删除).希望对您有所帮助.
from your code above I think you should remove ) before WHERE in the UPDATE command text. I hope this will help.
您似乎有一个额外的),并且在penerbit
之后错过了等号
还有另一个观察结果:不要将值直接连接到SQL语句,因为这会使您容易受到SQL注入,数据类型转换问题等的影响.而是使用 MySqlParameter [
You seem to have an extra ) and you miss equal sign afterpenerbit
And another observation: Don''t concatenate values directly to your SQL statement since this leaves you open to SQL injections, data type conversion problems and so on. Instead use MySqlParameter[^].
So your statement should look something like:
MySqlDataAdapter adp = new MySqlDataAdapter(@"
UPDATE tbukux
SET judulbuku = @judulbuku,
namakategori = @namakategori,
pengarang = @pengarang,
penerbit = @penerbit,
tahunterbit = @tahunterbit,
stokbuku = @stokbuku,
tglpenerimaan = @tglpenerimaan
WHERE kodebuku = @kodebuku", conn);
...
感谢Mika Wendelius,它确实有效...:)
thanks to Mika Wendelius, its really working... :)
这篇关于我无法更新我的mysql数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文