2016-07-07 40 views
-1

我正在使用vb.NET和數據庫AS DATABASE1.SDF創建數據庫更新程序。我想要動態更新DATAGridview中的數據庫。對於更新SQL命令我使用SQLCECOMMANDBUILDER,但我得到一個錯誤,因爲「DataAdapter.SelectCommand屬性需要初始化。」 這裏是我的代碼:更新vb.NeT中的數據庫時發生錯誤

Imports System.Data.OleDb 
Imports System.Data 
Imports System.Data.SqlServerCe 
Public Class Admin 
    Dim update As New SqlCeDataAdapter 
    ' sql connection strings 
    Dim SQLCon As String = "Data Source=Database1.sdf" 

    Dim sqlstr As String = "Select * from Base_Plate " 
    Dim sqlstr1 As String = "Select * from Alloy " 
    Dim sqlstr2 As String = "Select * from Bead_Factor " 
    Dim sqlstr3 As String = "Select * from Difficulty_Factor " 
    Dim sqlstr4 As String = "Select * from Price_Factor " 

    ' sql variable of base 
    Dim adapter As New SqlCeDataAdapter(sqlstr, SQLCon) 
    Dim ds As New DataSet() 

    ' sql variable of alloy 
    Dim adapter1 As New SqlCeDataAdapter(sqlstr1, SQLCon) 
    Dim ds1 As New DataSet() 

    ' sql variable of bead 
    Dim adapter2 As New SqlCeDataAdapter(sqlstr2, SQLCon) 
    Dim ds2 As New DataSet() 

    'sql variable of difficulty 
    Dim adapter3 As New SqlCeDataAdapter(sqlstr3, SQLCon) 
    Dim ds3 As New DataSet() 

    'sql variable of price 
    Dim adapter4 As New SqlCeDataAdapter(sqlstr4, SQLCon) 
    Dim ds4 As New DataSet() 

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click 

    End Sub 

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click 

     update.Update(ds) 
     LoadGrid() 

    End Sub 

    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click 
     Me.Visible = False 
     LoginForm1.Visible = True 
    End Sub 

    Private Sub Admin_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load 
     LoadGrid() 
     Button2.Enabled = False 

    End Sub 
    Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click 
    End Sub 
    Private Sub DataGridView1_CellValueChanged(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellValueChanged 
     Button2.Enabled = True 
    End Sub 

    Private Sub LoadGrid() 


     '************** base datagrid ******************** 

     adapter.Fill(ds, "Base_Plate") 
     DataGridView1.DataSource = ds.Tables(0) 
     DataGridView1.Rows(0).Selected = True 

     '***************** alloy datagrid ********************* 

     adapter1.Fill(ds1, "Alloy") 
     DataGridView2.DataSource = ds1.Tables(0) 
     DataGridView2.Rows(0).Selected = True 

     '***************** bead datagrid ********************* 

     adapter2.Fill(ds2, "Bead_Factor") 
     DataGridView3.DataSource = ds2.Tables(0) 
     DataGridView3.Rows(0).Selected = True 

     '***************** difficulty datagrid ********************* 

     adapter3.Fill(ds3, "Difficulty_Factor") 
     DataGridView4.DataSource = ds3.Tables(0) 
     DataGridView4.Rows(0).Selected = True 

     '***************** Price datagrid ********************* 

     adapter4.Fill(ds4, "Price_Factor") 
     DataGridView5.DataSource = ds4.Tables(0) 
     DataGridView5.Rows(0).Selected = True 

     update.UpdateCommand = New SqlServerCe.SqlCeCommandBuilder(update).GetUpdateCommand 
    End Sub 
+0

請不要寫全部大寫 - 首先,它很難閱讀,其次它被認爲是在你的觀衆面前喊叫**,這簡直粗魯無禮。 –

回答

0

你做錯了。首先,如錯誤消息所述,您尚未設置要傳遞給命令構建器構造函數的數據適配器的SelectCommand。下面是從你的代碼中的相關片段:

Dim update As New SqlCeDataAdapter 

update.UpdateCommand = New SqlServerCe.SqlCeCommandBuilder(update).GetUpdateCommand 

正如你所看到的,無處你提供一個SQL語句SELECT所以命令製造商應該如何知道如何建立其他的命令?

其次,即使被正確配置適配器,這是錯誤的:

update.UpdateCommand = New SqlServerCe.SqlCeCommandBuilder(update).GetUpdateCommand 

你不必從命令生成器獲取的命令,並將其分配給數據適配器的屬性。這樣做的唯一理由是如果你想編輯它們,你不這樣做。在創建數據適配器後立即創建命令生成器的正確方法就是例如

Dim myDataAdapter As New SqlCeDataAdapter(query, myConnection) 
Dim myCommandBuilder As New SqlCeCommandBuilder(myDataAdapter) 

就是這樣。您只創建一次,然後數據適配器將在您撥打Update時正常工作。每次保存都沒有創建新的命令,也沒有獲取和分配命令。

+0

Thanx。我已經解決了這個問題。但是,你的解決方案。對不起,我的帽子已經損壞了。 –