C#MySqlParameter问题

时间:2010-02-23 22:35:01

标签: c# .net mysql mysql-parameter

(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();

1 个答案:

答案 0 :(得分:4)

我没有使用MySql大约6个月(谢天谢地迁移到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();