OleDb在Access中插入一个与Parameter值不同的整数值
本文关键字:Parameter 整数 一个 Access 插入 OleDb | 更新日期: 2023-09-27 18:06:52
查询是插入select语句,其中参数是从csv文件生成的。查询失败,因为它找不到其中两个表之间的关系。表格定义如下:
Users
----------
AgentID <PK>(Long Integer)
....
Chages
----------
ChangeID <PK>(Auto Number)
AgentID <FK>(Long Integer)
....
CCD_ 1和插入到CCD_ 2中的值之间的关系可以在CCD_。
查询如下:
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.AgentID
是long
数据类型。
我可以将查询放在Access中,用值替换参数,它可以毫无问题地插入。
关于为什么这不起作用,有什么想法吗?
EDIT:我把数据库中的AgentID
值硬编码到参数中,但它仍然给出了错误。
我能够重新创建您的问题。这很奇怪。
OleDb似乎被... SELECT @AID AS AgentID ...
弄糊涂了。不管我给那个参数(例如1
(的值是多少,它总是作为41641
插入到数据库中。当我尝试使用Users.AgentID = Changes.AgentID
0时,我在尝试执行查询时遇到了"数据类型不匹配"异常。
这似乎适用于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();
}
根据http://msdn.microsoft.com/en-us/library/system.data.oledb.oledbtype.aspx,是一个OleDbType.Integer是一个32位有符号整数。
但是一个c#长,是一个64大的有符号整数http://msdn.microsoft.com/en-us/library/ctetwysk.aspx