1

我想创建一个可以注册人员信息的应用程序。但我在更新/编辑网格视图中的数据时遇到问题。下面是我创建的一组代码。

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()

4

1 回答 1

1

我不确定到底出了什么问题,我只能想象当您尝试使用它时connectionStringI会以某种方式Null出现这种情况,但我认为每次使用完后关闭连接是一种很好的做法。

因此,当您获取数据而不是像现在这样保持连接打开时,请执行以下操作

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

这样,您的连接将始终关闭,您不必担心检查它是否已经打开。

更新

我冒昧地按照我的方式重写了你的代码。你能试试这个,看看它是否适合你/

您连接字符串属性:

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]=@Name WHERE [ID]=@ID;"
                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

如果您将连接字符串设置为(只读)属性,假设它不会更改,那么您在使用它时不必担心它是否具有值。那么您所需要担心的就是确保连接字符串是正确的。

请注意,我尚未测试此代码,但它应该可以按我的想法工作。

于 2013-03-15T05:26:06.513 回答