将值文本框插入mysql数据库 [英] insert value textbox into mysql database
问题描述
我在将textboxvalue插入mysql数据库时遇到问题-没有错误消息,也没有插入.我在做什么错
I have problems inserting a textboxvalue into a mysql database - there's no errormessage and no inserting. What am I doing wrong
private void RegisterCustomer()
{
string firstname = txtfirstname.ToString();
OdbcConnection conn;
conn = new OdbcConnection(ConfigurationManager.ConnectionStrings["jConnString"].ConnectionString);
conn.Open();
string sql = "insert into klant (firstname) values (@firstname)";
OdbcCommand cmd = new OdbcCommand(sql, conn);
cmd.Parameters.Add("@firstname", OdbcType.VarChar).Value = firstname;
try
{
cmd.ExecuteNonQuery();
}
catch (Exception ex)
{
Check.Text += ex.ToString() + sql;
}
finally
{
conn.Close();
conn.Dispose();
Check.Text += "OK";
}
}
推荐答案
根据MSDN.
http://msdn.microsoft.com/en -us/library/system.data.odbc.odbccommand.parameters.aspx
According to MSDN.
http://msdn.microsoft.com/en-us/library/system.data.odbc.odbccommand.parameters.aspx
当CommandType设置为Text时,用于ODBC的.NET Framework数据提供程序不支持将命名参数传递给SQL语句或OdbcCommand调用的存储过程.在这两种情况下,均使用问号(?)占位符.
When CommandType is set to Text, the .NET Framework Data Provider for ODBC does not support passing named parameters to an SQL statement or to a stored procedure called by an OdbcCommand. In either of these cases, use the question mark (?) placeholder.
所以您的查询应该是:
string sql = "insert into klant (firstname) values (?)"
string sql = "insert into klant (firstname) values (?)"
如果您有多个参数,则会按照添加它们的顺序进行设置.
If you have multiple parameters, they are set in the order you add them.
此外,我认为这行
string firstname = txtfirstname.ToString();
应阅读
string firstname = txtfirstname.Text();
但这不是引起您直接问题的原因.
But that is not what is causing your immediate problem.
这篇关于将值文本框插入mysql数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!