我遇到了一些麻烦伙计们。经过几天的劳动,调试和研究,我在倒数第三行,我卡住了。这不是完整的代码,而是相关部分。
Dim dbProvider As String
Dim dbSource As String
Dim con As New OleDb.OleDbConnection
Dim ds As New DataSet
Dim MaxRows As Integer
Dim sql As String
Dim TableName As String
TableName = TbTableName.Text
Dim da As New OleDb.OleDbDataAdapter("SELECT * FROM [" & TableName & "]", con)
Dim cb As New OleDb.OleDbCommandBuilder(da)
Dim dsNewRow As DataRow
Dim dsNewColoumn As DataColumn
dbProvider = "PROVIDER=Microsoft.Jet.OLEDB.4.0;"
dbSource = "Data Source = E:\A2 Computing\Project\PasswordDatabase.mdb"
con.ConnectionString = dbProvider & dbSource
con.Open()
Dim TableCreate As New OleDb.OleDbCommand("CREATE TABLE [" & TableName & "](" & "ID INTEGER NOT NULL" & ")", con)
Dim NewColoumn As New OleDb.OleDbCommand("ALTER TABLE [" & TableName & "] ADD " & X & " VARCHAR(60)", con)
TableCreate.ExecuteNonQuery()
da.Fill(ds, "NewTable")
MaxRows = ds.Tables("NewTable").Rows.Count
ds.Tables("NewTable").PrimaryKey = New DataColumn() {ds.Tables("NewTable").Columns("CustID")}
X = 0
Do
X = X + 1
dsNewColoumn = ds.Tables("NewTable").Columns.Add
ds.Tables("NewTable").Columns.Add(X)
dsNewRow = ds.Tables("NewTable").NewRow()
ds.Tables("NewTable").Rows.Add(dsNewRow)
Loop Until X = 30
da.InsertCommand = cb.GetInsertCommand()
da.UpdateCommand = cb.GetUpdateCommand()
da.Update(ds, "NewTable")
End Sub
我遇到的问题在这里
da.UpdateCommand = cb.GetUpdateCommand()
错误是
对于不返回任何键列信息的 SelectCommand,不支持为 UpdateCommand 生成动态 SQL。
我知道这意味着我的表没有主键,但我已经设置了一个。任何帮助将不胜感激!=)