2013-03-15 116 views
1

我想創建一個應用程序,我可以註冊人員信息。 但我更新/編輯gridview中的數據時出現問題。 下面是我創建的一組代碼。無法更新/編輯數據庫

Imports System.Data.SqlClient 
Public Class Form1 
Dim connectionString As String 
Dim cnn As SqlConnection 
Dim cmd As SqlCommand 
Dim sql As String 
下面

是我的ADD聲明,我的更新語句巫婆我遇到它工作得很好

Private Sub btnADD_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnADD.Click 

    connectionString = "Data Source=XXXX;Initial Catalog=XXXX;User ID=XXXX;Password=XXXX" 
    cnn = New SqlConnection(connectionString) 
    Try 
     cnn.Open() 
     cmd = New SqlCommand("INSERT INTO tbl_info (ID,Name) VALUES (@ID,@Name)") 
     cmd.Connection = cnn 
     With cmd.Parameters 
      .AddWithValue("ID", txtID.Text) 
      .AddWithValue("Name", txtName.Text) 
     End With 
     cmd.ExecuteNonQuery() 
     MsgBox("has been inserted successfully") 

    Catch ex As Exception 
     MsgBox(ex.Message()) 
    End Try 


    txtID.Clear() 
    txtName.Clear() 
End Sub 
下面

的gridview的女巫是鏈接到我的數據庫

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load 
    'TODO: This line of code loads data into the 'TestDataSet.tbl_info' table. You can move, or remove it, as needed. 
    Me.Tbl_infoTableAdapter.Fill(Me.TestDataSet.tbl_info) 

End Sub 
下面

很難弄清楚什麼是錯的。

Private Sub btnEdit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEdit.Click 
    cnn = New SqlConnection(connectionString) 
    If Not cnn.State = ConnectionState.Open Then 
     cnn.Open() 
    End If 

    Try 
     cmd = New SqlCommand("Update tble_info set Name = @Name Where ID = @ID)") 
     cmd.Connection = cnn 
     With cmd.Parameters 
      .AddWithValue("Name", txtName.Text) 
      .AddWithValue("ID", txtID.Text) 
     End With 
     cmd.ExecuteNonQuery() 
     MsgBox("has been update successfully") 
    Catch ex As Exception 
     MsgBox(ex.Message()) 
    End Try 
    End Sub 
End Class 

,這是我遇到的錯誤,當我執行程序

InvalidOperationExeption was unhadled 
The connectionString property has not been initialize 

其指向cnn.open()

+0

當你得到錯誤時''connectionString'的值是什麼? – 2013-03-15 05:27:19

+0

指向cnn.open() – 2013-03-15 05:35:38

+0

'connectionString'在那一點的價值是什麼?這是你想要的嗎? – 2013-03-15 05:36:43

回答

1

我不知道究竟是錯的,我可以只是想象一下connectionStringI在某種程度上是Null,但我認爲每次使用它時關閉連接是一種很好的做法。

所以,當你獲取數據,而不是讓連接打開,你現在要做的,這樣做

With New SqlConnection(connectionString) 
    Try 
     .Open() 
     ' Do Stuff 
    Catch 
     ' Handle your exception 
    Finally 
     .Close() 
    End Try 
End With 

這樣,你的連接將始終被關閉,你將不必擔心是否檢查或者不是已經打開。

UPDATE

我已重寫你的代碼我會做到這一點的自由。能否請您給這一個嘗試,看看它是否適合你/

您連接字符串屬性:

Private ReadOnly Property connectionString As String 
    Get 
     Return "Data Source=XXXX;Initial Catalog=XXXX;User ID=XXXX;Password=XXXX" 
    End Get 
End Property 

其中插入數據的子:

Private Sub btnADD_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnADD.Click 

    Dim transaction As SqlTransaction = Nothing 

    With New SqlConnection(connectionString) 
     Try 
      Call .Open() 
      transaction = .BeginTransaction() 
      With .CreateCommand() 
       .Transaction = transaction 
       .CommandText = "INSERT INTO [tbl_info] ([ID], [Name]) VALUES (@ID,@Name);" 
       With .Parameters 
        .AddWithValue("ID", txtID.Text) 
        .AddWithValue("Name", txtName.Text) 
       End With 
       Call .ExecuteNonQuery() 
       Call transaction.Commit() 
       Call MessageBox.Show("has been inserted successfully") 
      End With 
     Catch ex As Exception 
      Call transaction.Rollback() 
      Call MessageBox.Show(ex.Message, "Error") 
     Finally 
      Call .Close() 
     End Try 
    End With 

    Call txtID.Clear() 
    Call txtName.Clear() 

End Sub 

子其更新數據:

Private Sub btnEdit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEdit.Click 

    Dim transaction As SqlTransaction = Nothing 

    With New SqlConnection(connectionString) 
     Try 
      Call .Open() 
      transaction = .BeginTransaction() 
      With .CreateCommand() 
       .Transaction = transaction 
       .CommandText = "UPDATE [tble_info] SET [Name][email protected] WHERE [ID][email protected];" 
       With .Parameters 
        .AddWithValue("Name", txtName.Text) 
        .AddWithValue("ID", txtID.Text) 
       End With 
       Call .ExecuteNonQuery() 
       Call transaction.Commit() 
       Call MessageBox.Show("has been update successfully") 
      End With 
     Catch ex As Exception 
      Call transaction.Rollback() 
      Call MessageBox.Show(ex.Message, "Error") 
     Finally 
      Call .Close() 
     End Try 
    End With 

End Sub 

如果您將連接字符串設置爲只讀屬性,假設它沒有'當你使用它時,你不必擔心它是否有價值。然後,所有你必須擔心的是確保連接字符串是正確的。

請注意,我沒有測試過這個代碼,但它應該按我的想法工作。

+0

我試試看。沒有什麼變化發生它顯示相同的錯誤[InvalidOperationExeption被解開 connectionString屬性尚未初始化] – 2013-03-16 03:16:55

+0

你已經驗證,當初始化連接時'connectionString'不是null?正在開發32位還是64位? – 2013-03-16 05:08:09

+0

我使用32位。感謝修改我的代碼。它工作。我不知道該說什麼,我很高興。非常感謝您爲幫助我做出這樣的努力。 – 2013-03-18 01:29:46