|
-
Apr 20th, 2002, 11:08 AM
#1
Thread Starter
Lively Member
inserting time stamp into database
Here'e the code:
Code:
Dim objConn As New OleDbConnection(strDSN)
Dim objCmd As New OleDbCommand("nwsInsert", objConn)
objCmd.CommandType = CommandType.StoredProcedure
'get the input time
Dim dttDate As DateTime
dttDate = Date.Now.UtcNow
objParam = New OleDbParameter("@postedon", OleDbType.DBTimeStamp)
objParam.Value = dttDate
objCmd.Parameters.Add(objParam)
'execute the append
objCmd.Connection.Open()
objCmd.ExecuteNonQuery()
objCmd.Connection.Close()
objConn.Dispose()
I'm trying to access an Access database, and the @postedon parameter is supposed to be inserted into a field of type Date/Time and the format is General Date. I tried various OleDbType - DBTimeStamp, DBDate, Date, DBTime, Char - all resulted in "System.Data.OleDb.OleDbException: Data type mismatch in criteria expression." What is the right way to add dates?
-
Apr 21st, 2002, 08:36 AM
#2
I created a table(myTable) with two columns('myId' as Numeric and 'myTimeStamp' as Date/Time). Created an action query(qryMyTable_Insert) that took two params to do the insert. Following code executes the query:
VB Code:
Public Sub timeStampTest()
Dim sConnString As String = _
"Provider=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source=E:\Progs\DotNet\adosql7test\test.mdb;"
Dim oleCn As OleDb.OleDbConnection
Dim sCmdText As String
Dim oleCmd As OleDb.OleDbCommand
Dim oleParam As OleDb.OleDbParameter
oleCn = New OleDb.OleDbConnection(sConnString)
Try
oleCn.Open()
sCmdText = "qryMyTable_Insert"
oleCmd = New OleDb.OleDbCommand(sCmdText, oleCn)
oleCmd.CommandType = CommandType.StoredProcedure
oleParam = New OleDb.OleDbParameter("@newId", OleDbType.Numeric)
oleParam.Value = 5
oleCmd.Parameters.Add(oleParam)
oleParam = New OleDb.OleDbParameter("@newTimeStamp", OleDbType.Date)
oleParam.Value = Date.Now()
oleCmd.Parameters.Add(oleParam)
oleCmd.ExecuteNonQuery()
Catch oleEx As OleDbException
MessageBox.Show(oleEx.Message.ToString())
Finally
oleCmd.Dispose()
oleCn.Close()
oleCn.Dispose()
End Try
MessageBox.Show("Done!")
End Sub
-
Apr 21st, 2002, 08:00 PM
#3
Thread Starter
Lively Member
-
Apr 21st, 2002, 08:56 PM
#4
my query is the following:
Code:
INSERT INTO myTable ( myId, myTimeStamp )
VALUES ([newId], [newTimeStamp]);
...and again the 'myId' field is Numeric and the 'myTimeStamp' is Date/Time. I'm using MS Access 2000 format if that makes a difference. I haven't upgraded from Beta 2 yet so maybe if you're on the final release something's changed.
-
Apr 21st, 2002, 09:05 PM
#5
Thread Starter
Lively Member
more oledbtype
I used a loop to check the data type name using objReader.GetDataTypeName and found out that:
1) a field of type Text is DBTYPE_WVARCHAR, while
2) a field of type Memo is DBTYPE_WLONGVARCHAR.
So, I used OleDbType.VarWChar and OleDbType.LongVarWChar instead of OleDbType.Char. Still, it results in Data type mismatch in criteria expression.... what else could result in such an exception ???
-
Apr 28th, 2002, 08:17 PM
#6
Thread Starter
Lively Member
mystery solved
I have finally solved the problem. The types are correct - but that is not the problem. The problem is somehow the order of adding parameters matter. That is, if your SQL statement is:
Code:
INSERT INTO table (field1, field2) VALUES (@field1, @field2)
the parameters addition must also follow the same order:
Code:
Dim objParam as New OleDbParameter("@field1", OleDbType.Integer
objParam.Value = 0
objCmd.Parameters.Add(objParam)
objParam as New OleDbParameter("@field2", OleDbType.Char
objParam.Value = "testing"
objCmd.Parameters.Add(objParam)
and this wouldn't work! :
Code:
Dim objParam as New OleDbParameter("@field2", OleDbType.Char
objParam.Value = "testing"
objCmd.Parameters.Add(objParam)
objParam as New OleDbParameter("@field1", OleDbType.Integer
objParam.Value = 0
objCmd.Parameters.Add(objParam)
I hope this helps for anyone stuck with the same problem that I had for 2 days!
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|