OleDb在Access中插入一个不同于Parameter值的整数值

时间:2014-06-05 03:37:21

标签: c# sql ms-access oledb ms-access-2013

查询是select语句的插入,其中参数是从csv文件生成的。查询失败,因为它无法找到两个表之间的关系。表定义如下:

Users
----------
AgentID <PK>(Long Integer)
....

Chages
----------
ChangeID <PK>(Auto Number)
AgentID <FK>(Long Integer)
....

关系介于Users.AgentID = Changes.AgentID之间,Changes中插入的值可以在Users中找到。

查询如下:

    string sqlInsertChange = "INSERT INTO Changes" +
                                "(AgentID, ChangeReasonID, RecDateID, EffDateID) " +
                             "SELECT " +
                                "@AID AS AgentID, C.ChangeReasonID, " +
                                "(SELECT D.DateID FROM DateCodes AS D WHERE D.DateValue = @RD) AS RecDateID, " +
                                "(SELECT D.DateID FROM DateCodes AS D WHERE D.DateValue = @ED) AS EffDateID " +
                             "FROM " +
                                "ChangeReasons AS C " +
                             "WHERE " +
                                "C.ChangeReason = @CR;";



    UpdateChageCmd = new OleDbCommand(sqlInsertChange, conn);

    UpdateChageCmd.Parameters.Add("@AID", OleDbType.Integer);
    UpdateChageCmd.Parameters.Add("@RD", OleDbType.Date);
    UpdateChageCmd.Parameters.Add("@ED", OleDbType.Date);
    UpdateChageCmd.Parameters.Add("@CR", OleDbType.VarWChar);


    UpdateChageCmd.Parameters["@AID"].Value = chg.AgentID;
    UpdateChageCmd.Parameters["@RD"].Value = chg.recDate;
    UpdateChageCmd.Parameters["@ED"].Value = chg.effDate;
    UpdateChageCmd.Parameters["@CR"].Value = chg.reason;

    UpdateChageCmd.ExecuteNonQuery();

chg.AgentIDlong数据类型。

我可以将查询放在Access中,用值替换参数,并且插入时没有任何问题。

关于为什么这不起作用的任何想法?

编辑:我将我知道在数据库中的AgentID值硬编码到参数中,但它仍然给出了错误。

2 个答案:

答案 0 :(得分:1)

根据http://msdn.microsoft.com/en-us/library/system.data.oledb.oledbtype.aspx,OleDbType.Integer是一个32位有符号整数。

但是一个c#long,是一个64大的有符号整数 http://msdn.microsoft.com/en-us/library/ctetwysk.aspx

答案 1 :(得分:1)

我能够重新创建你的问题。这很奇怪。

似乎OleDb被... SELECT @AID AS AgentID ...弄糊涂了。无论我给该参数赋予什么值(例如1),它总是以41641的形式插入到数据库中。当我尝试使用.AddWithValue时,当我尝试执行查询时出现“数据类型不匹配”异常。

这似乎适用于OleDb,但它有点难看:

string sqlInsertChange = 
        "INSERT INTO Changes" +
            "(AgentID, ChangeReasonID, RecDateID, EffDateID) " +
        "SELECT " +
            chg.AgentID + " AS AgentID, C.ChangeReasonID, " +
            "(SELECT D.DateID FROM DateCodes AS D WHERE D.DateValue = @RD) AS RecDateID, " +
            "(SELECT D.DateID FROM DateCodes AS D WHERE D.DateValue = @ED) AS EffDateID " +
        "FROM " +
            "ChangeReasons AS C " +
        "WHERE " +
            "C.ChangeReason = @CR;";

using (var UpdateChageCmd = new OleDbCommand(sqlInsertChange, conn))
{
    //UpdateChageCmd.Parameters.Add("@AID", OleDbType.Integer);
    UpdateChageCmd.Parameters.Add("@RD", OleDbType.Date);
    UpdateChageCmd.Parameters.Add("@ED", OleDbType.Date);
    UpdateChageCmd.Parameters.Add("@CR", OleDbType.VarWChar);

    //UpdateChageCmd.Parameters["@AID"].Value = chg.AgentID;
    UpdateChageCmd.Parameters["@RD"].Value = chg.recDate;
    UpdateChageCmd.Parameters["@ED"].Value = chg.effDate;
    UpdateChageCmd.Parameters["@CR"].Value = chg.reason;

    UpdateChageCmd.ExecuteNonQuery();
}

或者,如果您愿意从OleDb切换到Odbc,那么这也适用:

string sqlInsertChange = 
        "INSERT INTO Changes" +
            "(AgentID, ChangeReasonID, RecDateID, EffDateID) " +
        "SELECT " +
            "? AS AgentID, C.ChangeReasonID, " +
            "(SELECT D.DateID FROM DateCodes AS D WHERE D.DateValue = ?) AS RecDateID, " +
            "(SELECT D.DateID FROM DateCodes AS D WHERE D.DateValue = ?) AS EffDateID " +
        "FROM " +
            "ChangeReasons AS C " +
        "WHERE " +
            "C.ChangeReason = ?;";

using (var UpdateChageCmd = new OdbcCommand(sqlInsertChange, conn))
{
    UpdateChageCmd.Parameters.Add("?", OdbcType.Int);
    UpdateChageCmd.Parameters.Add("?", OdbcType.DateTime);
    UpdateChageCmd.Parameters.Add("?", OdbcType.DateTime);
    UpdateChageCmd.Parameters.Add("?", OdbcType.NVarChar);

    UpdateChageCmd.Parameters[0].Value = chg.AgentID;
    UpdateChageCmd.Parameters[1].Value = chg.recDate;
    UpdateChageCmd.Parameters[2].Value = chg.effDate;
    UpdateChageCmd.Parameters[3].Value = chg.reason;

    UpdateChageCmd.ExecuteNonQuery();
}