mysql command type,帮助我在C#中的MySqlCommand参数

//What's wrong in code !!!protected void Button1_Click(object sender, EventArgs e)

{

MySqlConnection conx;

conx = new MySqlConnection("server=localhost; database=pt; user id=root;");

MySqlCommand SimpanAdmin = new MySqlCommand("insert into adminPT (nama,katasandi) Values (@A,@AB)", conx);

SimpanAdmin.Parameters.Add("@A", MySqlDbType.VarChar,50);

SimpanAdmin.Parameters.Add("@AB", MySqlDbType.VarChar,45);

SimpanAdmin.Parameters["@A"].Value = TextBox1.Text;

SimpanAdmin.Parameters["@AB"].Value = TextBox2.Text;

chy.Text = "Proses Menyimpan Berhasil...!!!";

try

{

SimpanAdmin.Connection.Open();

SimpanAdmin.ExecuteNonQuery();

SimpanAdmin.Connection.Close();

}

catch

{

chy.Text = "Proses Menyimpan Gagal...!!!";

}

}

// Can you help me for wrong code...i am finish for build but cannot save in MySql database :confused::confused:

解决方案You try the following code ,It may be helpful

protected void Button1_Click(object sender, EventArgs e)

{

MySqlConnection conx;

conx = new MySqlConnection("server=localhost; database=pt; user id=root;");

try

{

MySqlCommand SimpanAdmin = new MySqlCommand("insert into adminPT (nama,katasandi) Values (@A,@AB)", conx);

SimpanAdmin.Connection.Open();

SimpanAdmin.Parameters.Add("@A", MySqlDbType.VarChar, 50).Value = TextBox1.Text;

SimpanAdmin.Parameters.Add("@AB", MySqlDbType.VarChar, 45).Value = TextBox2.Text;

chy.Text = "Proses Menyimpan Berhasil...!!!";

SimpanAdmin.ExecuteNonQuery();

}

catch

{

chy.Text = "Proses Menyimpan Gagal...!!!";

}

finally { SimpanAdmin.Connection.Close();

}

}

Best Regards,

Theingi Win.

Hello,

I have the same problem, my solution was to change @ to ? like:

protected void Button1_Click(object sender, EventArgs e)

{

MySqlConnection conx;

conx = new MySqlConnection("server=localhost; database=pt; user id=root;");

MySqlCommand SimpanAdmin = new MySqlCommand("insert into adminPT (nama,katasandi) Values (?A,?AB)", conx);

SimpanAdmin.Parameters.Add("?A", MySqlDbType.VarChar,50).Value = TextBox1.Text;

SimpanAdmin.Parameters.Add("?AB", MySqlDbType.VarChar,45).Value = TextBox2.Text;

chy.Text = "Proses Menyimpan Berhasil...!!!";

try

{

SimpanAdmin.Connection.Open();

SimpanAdmin.ExecuteNonQuery();

SimpanAdmin.Connection.Close();

}

catch

{

chy.Text = "Proses Menyimpan Gagal...!!!";

}

}

HTH

Old question but from here:

http://dev.mysql.com/doc/refman/5.0/es/connector-net-examples-mysqlcommand.html[^]

Note. Prior versions of the provider used the '@' symbol to mark parameters in SQL. This is incompatible with MySQL user variables, so the provider now uses the '?' symbol to locate parameters in SQL. To support older code, you can set 'old syntax=yes' on your connection string. If you do this, please be aware that an exception will not be throw if you fail to define a parameter that you intended to use in your SQL.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值