如果习惯了ORACLE的Sequence,到ACCESS下使用自动编号字段还是会不习惯:如何取得新的编号值,如何拿他来做关联字段值?
可以做如下一个函数:
''' <summary>
''' 取得序列的下一值,用于处理自动编号字段,库表结构为SeqTable(IDField,AA),AA为数字型字段
''' </summary>
''' <param name="strSeqTable"></param>
''' <returns></returns>
''' <remarks></remarks>
Public Shared Function GetSeqValue(ByVal theOleDbAccess As OleDbAccess, ByVal strSeqTable As String) As Object
Try
Dim aCon As OleDbConnection = CType(theOleDbAccess.OpenConnection(CType(aCon, IDbConnection)), OleDbConnection)
Dim strSQL1 As String = "insert into " & strSeqTable & "(AA)values(1)"
Dim strSQL2 As String = "SELECT @@IDENTITY"
Dim aCmd As New OleDbCommand(strSQL1, aCon)
aCmd.ExecuteNonQuery()
aCmd.Dispose()
Dim aCmd2 As New OleDbCommand(strSQL2, aCon)
Return aCmd2.ExecuteScalar()
Catch ex As Exception
modOutputLog.OutputLog(ex)
Throw ex
End Try
End Function
''' 取得序列的下一值,用于处理自动编号字段,库表结构为SeqTable(IDField,AA),AA为数字型字段
''' </summary>
''' <param name="strSeqTable"></param>
''' <returns></returns>
''' <remarks></remarks>
Public Shared Function GetSeqValue(ByVal theOleDbAccess As OleDbAccess, ByVal strSeqTable As String) As Object
Try
Dim aCon As OleDbConnection = CType(theOleDbAccess.OpenConnection(CType(aCon, IDbConnection)), OleDbConnection)
Dim strSQL1 As String = "insert into " & strSeqTable & "(AA)values(1)"
Dim strSQL2 As String = "SELECT @@IDENTITY"
Dim aCmd As New OleDbCommand(strSQL1, aCon)
aCmd.ExecuteNonQuery()
aCmd.Dispose()
Dim aCmd2 As New OleDbCommand(strSQL2, aCon)
Return aCmd2.ExecuteScalar()
Catch ex As Exception
modOutputLog.OutputLog(ex)
Throw ex
End Try
End Function
2.字段名的保留字
开始从ORACLE转到ACCESS,因为要做一单机版程序,并要导数据到ORACLE上,因此采用了和ORACLE库表一样的结构,如下:
UID,PhoneNo,Address,Password
一切SELECT都正常,我手工往里加了数据,访问也正常,直到我想从ORACLE导数据并insert到ACCESS时,出错了:无效的INSERT语句!
我更新用的是oleDBDataAdapter生成的SQL来更新的,到我改成手工的:
insert into AAAA(UID,PhoneNo,Address,Password) values(xxxxxxx)也还是不行。
我用上面的SQL在ACCESS中构造了一个查询,又是可以执行的,怪了。
后来一个字段一个字段去掉,才发现,原来PASSWORD是OLEDB的一个保留字,拿来做字段名,写在SQL中要转义,写成
insert into AAAA(UID,PhoneNo,Address,[Password]) values(xxxxxxx),就正常了。
这几个问题忙得实在不太值,都是一些约定上的东西,要自己一点一点分析出来,效率太低了。