连接到SQL Server(Express)时出现问题

时间:2019-01-15 16:55:21

标签: sql-server vb.net

使用VB.NET和Visual Studio2008。通过下面的代码,我得到

  

找不到表0

Public ConnString As String = "Data Source=NOD\SQLEXPRESS;Initial Catalog=dbaseManipulation;Integrated Security=True"

    Public Function ExecuteSQLStatement(ByVal SQLString As String, ByVal myDataGrid As DataGridView)
        Dim sqlconn As New SqlClient.SqlConnection(ConnString)
        Dim sqlDataAdapter As New SqlClient.SqlDataAdapter
        Dim myDataSet As New DataSet
        sqlconn.Open()
        Try
            sqlDataAdapter.SelectCommand = New SqlClient.SqlCommand(SQLString, sqlconn)
            sqlDataAdapter.Fill(myDataSet)
            myDataGrid.DataSource = myDataSet.Tables(0)
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
        sqlDataAdapter.Dispose()
        myDataSet.Dispose()
        sqlconn.Dispose()
        Return True
    End Function

2 个答案:

答案 0 :(得分:0)

更新,感谢Lars澄清了您正在执行INSERT。您的帮助请求中不清楚。

下面的代码可以作为一个干净的示例,但是请不要接受它作为答案。

Public Function ExecuteSQLStatement(ByVal SQLString As String, ByVal myDataGrid As DataGridView)

    Dim result As Boolean = True
    Dim myDataSet As DataSet = New DataSet("dsData")

    Try

        Using cnn As SqlConnection = New SqlConnection(ConnString)

            cnn.Open()

            Dim cmd As SqlCommand = New SqlCommand(SQLString, cnn)
            cmd.CommandType = CommandType.Text

            Dim sda As SqlDataAdapter = New SqlDataAdapter(cmd)
            sda.Fill(myDataSet)

        End Using

        myDataGrid.DataSource = myDataSet.Tables(0)

    Catch ex As Exception

        result = False
        MsgBox(ex.Message)

    End Try

    Return result

End Function

答案 1 :(得分:0)

在线评论和解释

Public ConnString As String = "Data Source=NOD\SQLEXPRESS;Initial Catalog=dbaseManipulation;Integrated Security=True"
'A Function must be declared with a DataType
Public Function InsertCourse() As Boolean
    Dim RetVal As Integer
    'Using blocks ensure that your database objects are closed and disposed
    'even if there is an error
    Using sqlconn As New SqlClient.SqlConnection(ConnString)
        'List the column names in the Insert that match the Values clause
        'I guessed at the field name CourseName. Correct with the actual name
        Using cmd As New SqlCommand("INSERT INTO Courses (CourseName) VALUES (@CourseName)", sqlconn)
            'Use Parameters - avoid SQL Injection and syntax errors
            'I guessed at the datatype. Check your database and adjust.
            cmd.Parameters.Add("@CourseName", SqlDbType.VarChar).Value = txtCourse.Text
            'Open the connection at the last minute, End Using will close and dispose it.
            sqlconn.Open()
            RetVal = cmd.ExecuteNonQuery
        End Using
    End Using

    If RetVal = 1 Then
        Return True
        'Try to keep your Subs and Function to a single responsibility
        UpdateGrid()
    Else
        Return False
    End If
End Function
Private Sub UpdateGrid()
    Dim dt As New DataTable()
    Using sqlconn As New SqlClient.SqlConnection(ConnString)
        Using cmd As New SqlCommand("Select * From Courses:", sqlconn)
            'The load method fills the data table and closes the reader
            dt.Load(cmd.ExecuteReader)
        End Using
    End Using
    DataGridView1.DataSource = Nothing
    DataGridView1.DataSource = dt
End Sub