[英]Null value getting stored in mySQL table when insert query fired from ASP.NET using mySQL odbc connector
After spending several hours i am unable to figure out that why null values are being inserted into mySQL table using ASP.NET web page. 花了几个小时后,我无法弄清楚为什么使用ASP.NET网页将空值插入到mySQL表中。 I am using odbc connector for this.Below is the code for the same. 我为此使用odbc连接器。以下是相同的代码。
public int Insert(string FirstName, string LastName, int age)
{
OdbcConnection conn = new OdbcConnection(connStr);
conn.Open();
OdbcCommand odcmd_Insert = new OdbcCommand("INSERT INTO webuse(firstName,lastName,age) VALUES(@param1,@param2,@param3)",conn);
odcmd_Insert.Connection = conn;
odcmd_Insert.CommandType = System.Data.CommandType.Text;
try
{
odcmd_Insert.Parameters.Add(new OdbcParameter( "@param1", FirstName));
odcmd_Insert.Parameters.Add(new OdbcParameter( "@param2", LastName));
odcmd_Insert.Parameters.Add( new OdbcParameter("@param3", age));
return odcmd_Insert.ExecuteNonQuery();
}
catch (OdbcException e)
{
throw;
}
finally {
odcmd_Insert.Dispose();
conn.Close();
conn.Dispose();
}
}
I have debugged the code and all things seems well but all columns are updated with null values. 我已经调试了代码,所有事情看起来都不错,但是所有列都更新为空值。 Please help i am a noob to ASP.NET. 请帮助我对ASP.NET不熟悉。
Please try your argument like as below. 请按如下所示尝试您的论点。 I have used the MySqlConnection. 我已经使用了MySqlConnection。 you can use ODBC connection as well. 您也可以使用ODBC连接。
try
{
// Connection string for a typical local MySQL installation
string cnnString = "Server=localhost;Port=3306;Database=ci_series;Uid=root;Pwd=";
// Create a connection object
MySqlConnection connection = new MySqlConnection(cnnString);
// Create a SQL command object
string cmdText = "INSERT INTO webuse(firstName,lastName,age) VALUES(?param1,?param2,?param3)";
MySqlCommand cmd = new MySqlCommand(cmdText, connection);
cmd.CommandType = CommandType.Text;
cmd.Parameters.Add("?param1", MySqlDbType.VarChar).Value = firstName;
cmd.Parameters.Add("?param2", MySqlDbType.VarChar).Value = lastName;
cmd.Parameters.Add("?param3", MySqlDbType.VarChar).Value = age;
connection.Open();
int result = cmd.ExecuteNonQuery();
}
catch (Exception ex)
{
}
The command should be as 该命令应为
string cmdText = "INSERT INTO webuse(firstName,lastName,age) VALUES(?param1,?param2,?param3)";
I think that your OdbcCommand should be (replace in query @paramN
with ?
) 我认为你的OdbcCommand应该是(询问更换@paramN
用?
)
OdbcCommand odcmd_Insert = new OdbcCommand("INSERT INTO webuse(firstName,lastName,age) VALUES(?,?,?)",conn);
odcmd_Insert.Parameters.Add(new OdbcParameter( "@param1", FirstName));
odcmd_Insert.Parameters.Add(new OdbcParameter( "@param2", LastName));
odcmd_Insert.Parameters.Add( new OdbcParameter("@param3", age));
Instead of the parameter it takes a ? 取而代之的是参数? in the CommandText (leave the name in the actual parameters param1,param2,param3) 在CommandText中(将名称保留在实际参数param1,param2,param3中)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.