我正在vb.net中创建一个数据库应用程序。我在visual studio中设置了一个标识列(是identity = true)。但是,当我运行我的应用程序时,不识别任何标识列。我哪里错了?
我的连接字符串是:
"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=C:\Users\Administrator\Desktop\SKEDULERING LOCAL DATABASE\SkeduleringLD1.0\SkeduleringLD1.0\Databasis\Skeddatabasis.mdf;Integrated Security=True"
我已将此设置为app.config文件。然后我打开连接:
Public konneksie As New
SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings("konneksie").ConnectionString)
连接确实已打开。但是标识列似乎是一个问题。
答案 0 :(得分:1)
Identity
是保留字,因此您需要在查询中使用[Identity]
进行调用,或者为该列使用其他名称。
有关详情,请参阅Reserved Keywords List
答案 1 :(得分:0)
这是我使用的通用实用方法......
假设你宣布了这个:
Friend Shared ConnectionString As String = ConfigurationManager.ConnectionStrings("konneksie").ToString
您可以将SQL Query和实例化的DataTable传递到此:
Friend Shared Sub LoadData(SQL As String, ByRef TableObject As DataTable, Optional Clear As Boolean = True)
If Clear Then TableObject.Clear()
Using SqlConnection As New SqlClient.SqlConnection(ConnectionString)
With New SqlClient.SqlDataAdapter(SQL, SqlConnection)
.SelectCommand.CommandTimeout = 60
.MissingSchemaAction = Data.MissingSchemaAction.AddWithKey
.FillSchema(TableObject, SchemaType.Source)
.Fill(TableObject)
.Dispose()
End With
End Using
For Each col As DataColumn In TableObject.PrimaryKey
With col
If .AutoIncrement Then
' Always set Step BEFORE Seed
.AutoIncrementStep = -1
.AutoIncrementSeed = -1
End If
End With
Next
End Sub