(int)faultsGroup为0或1,但我总是收到此错误:列’FaultGroup’不能为null
有人告诉我为什么吗?语法看起来还可以。
MySqlCommand cmdAdd = new MySqlCommand("INSERT INTO Faults (" + " FaultGroup, Text, Date, IP" + ") VALUES (" + " @FaultGroup, @Text, @Date, @IP" + ")", conn); MySqlParameter paramFaultGroup = new MySqlParameter("@FaultGroup", MySqlDbType.Int32); FaultsGroup faultsGroup = (FaultsGroup) Enum.Parse(typeof (FaultsGroup), myFault.FaultGroup); paramFaultGroup.Value = (int) faultsGroup; cmdAdd.Parameters.Add(paramFaultGroup); cmdAdd.ExecuteNonQuery();
我大约六个月没有使用MySql(感谢迁移到Sql Server),但是,请尝试将您的@符号更改为?,就像使用了内存一样,这是MySql的正确约定,因此:
MySqlCommand cmdAdd = new MySqlCommand( "INSERT INTO Faults (FaultGroup, Text, Date, IP)" + " VALUES (?FaultGroup, ?Text, ?Date, ?IP)", conn); MySqlParameter paramFaultGroup = new MySqlParameter("?FaultGroup", MySqlDbType.Int32); FaultsGroup faultsGroup = (FaultsGroup) Enum.Parse(typeof (FaultsGroup), myFault.FaultGroup); paramFaultGroup.Value = (int) faultsGroup; cmdAdd.Parameters.Add(paramFaultGroup); cmdAdd.ExecuteNonQuery();