简体   繁体   English

在Timestamp C#中使用db2 odbc参数

[英]Using db2 odbc parameters with Timestamp C#

I am trying to use parameters for a DB2 query using ODbc in C#, but the Timestamp field is giving me issues. 我正在尝试在C#中使用ODbc对DB2查询使用参数,但是Timestamp字段给我带来了问题。 I am using DB2 Client 9.7 and updateDate is a DateTime type. 我正在使用DB2 Client 9.7,并且updateDate是DateTime类型。 Here is my code: 这是我的代码:

string commandString = "INSERT INTO DATABASENAME(USERNAME, UPDATE_DATE) VALUES ('?', TIMESTAMP_FORMAT('?', 'YYYY-MM-DD HH24:MI:SS'))";

OdbcConnection con = new OdbcConnection(conString);
OdbcCommand command = new OdbcCommand(commandString, con);

command.Parameters.AddWithValue("?username", userName);
command.Parameters.AddWithValue("?update_date", updateDate.ToString("yyyy-MM-dd HH:mm:ss"));

//execute the stuff
con.Open();
command.ExecuteNonQuery();
con.Close();

This is the error I am receiving: 这是我收到的错误:

ERROR [42815] [IBM][CLI Driver][DB2] SQL0171N  The data type, length or value of the argument for the parameter in position "1" of routine "SYSIBM.TIMESTAMP_FORMAT" is incorrect. Parameter name: "".  SQLSTATE=42815

I have also tried using the regular TIMESTAMP() function included in DB2 using both formats it accepts (Ex. TIMESTAMP ('2016-10-20-12.00.00.000000') TIMESTAMP ('2016-10-20 12:00:00')), but that gives me this error: 我还尝试过使用DB2中包含的常规TIMESTAMP()函数,并使用它接受的两种格式(例如TIMESTAMP('2016-10-20-12.00.00.000000')TIMESTAMP('2016-10-20 12:00:00' )),但这给了我这个错误:

ERROR [22007] [IBM][CLI Driver][DB2] SQL0180N  The syntax of the string representation of a datetime value is incorrect.  SQLSTATE=22007

Anyone know where I am going wrong? 有人知道我要去哪里错吗?

EDIT: It works without using parameters, can parameters not be used with DB2? 编辑:它不使用参数就可以工作,参数不能与DB2一起使用吗?

I have no way of testing this but I think you should not enclose parameter markers in quotes, they are parameters, not literals. 我无法对此进行测试,但是我认为您不应将参数标记括在引号中,它们是参数,而不是文字。 Your statement should look like 您的陈述应如下所示

string commandString = "INSERT INTO DATABASENAME(USERNAME, UPDATE_DATE) VALUES (?,?)";

and then you also won't need double conversion from DateTime to string to TIMESTAMP : 然后您也不需要从DateTime到string到TIMESTAMP双重转换:

command.Parameters.AddWithValue("?username", userName);
command.Parameters.AddWithValue("?update_date", updateDate);

may be an error on timestamp format. 可能是时间戳格式错误。

Try to do updateDate.ToString("yyyy-MM-dd.HH.mm.ss.ffffff")); 尝试做updateDate.ToString(“ yyyy-MM-dd.HH.mm.ss.ffffff”));;

try it: 试试吧:

string commandString =string.format("INSERT INTO DATABASENAME(USERNAME, UPDATE_DATE) VALUES ('{0}', '{1}')", userName, updateDate.ToString("yyyy-MM-dd.HH.mm.ss.ffffff"));

OdbcConnection con = new OdbcConnection(conString);
OdbcCommand command = new OdbcCommand(commandString, con);

//execute the stuff
con.Open();
command.ExecuteNonQuery();
con.Close();
        string commandString = "INSERT INTO DATABASENAME(USERNAME, UPDATE_DATE) VALUES ('?','?')";

        OdbcConnection con = new OdbcConnection(conString);
        OdbcCommand command = new OdbcCommand(commandString, con);

        OdbcParameter p1 = command.CreateParameter();
        p1.DbType = DbType.AnsiString;
        p1.Value = userName;
        command.Parameters.Add(p1);

        OdbcParameter p2 = command.CreateParameter();
        p2.DbType = DbType.AnsiString;
        p2.Value = updateDate.ToString("yyyy-MM-dd HH:mm:ss");
        command.Parameters.Add(p2);


        con.Open();
        command.ExecuteNonQuery();
        con.Close();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM