花了几个小时后,我无法弄清楚为什么使用ASP.NET网页将空值插入到mySQL表中。我为此使用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();
}
}
我已经调试了代码,所有事情看起来都不错,但是所有列都更新为空值。请帮助我是ASP.NET的菜鸟。
我认为你的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));
取而代之的是参数?在CommandText中(将名称保留在实际参数param1,param2,param3中)
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句