编辑连接字符串时出问题

时间:2012-01-23 10:28:25

标签: sql-server vb.net

我正在使用以下代码编辑我的连接字符串

Dim adapter As New dbSQLServerTestDataSet1.tblContact
Dim conn As New SqlConnection()
conn.ConnectionString = "Data Source=" & vrCompName & "\SQLEXPRESS;AttachDbFilename=|DataDirectory|\dbSQLServerTest.mdf;Integrated Security=True;User Instance=True"
adapter.Connection = conn
Dim table As DataTable = adapter.GetData()

但它在

上出错
Dim adapter As New dbSQLServerTestDataSet1.tblContact

在该行的dbSQLServerTestDataSet1.tblContact部分。这些对象确实在我的项目中退出。我还导入了System.DataSystem.Data.SqlClient

请指导。

1 个答案:

答案 0 :(得分:0)

添加命名空间

Imports System.Data.SqlClient

然后使用以下代码:

 Dim ds As dsTestCon

    Me.taTestCon.Fill(Me.DsTestCon.tblTest)
    MsgBox(DsTestCon.tblTest.Rows(0).Item(1))

    MsgBox(taTestCon.Connection.ConnectionString)


    Dim adapter As New dsTestConTableAdapters.tblTestTableAdapter
    Dim conn As New SqlConnection()
    Dim vrCompName As String = My.Computer.Name
    conn.ConnectionString = "Data Source=" & vrCompName & "\SQLEXPRESS;AttachDbFilename=e:\dbTest.mdf;Integrated Security=True;User Instance=True"
    adapter.Connection = conn
    Dim table As DataTable = adapter.GetData()
    MsgBox(adapter.Connection.ConnectionString)

    ds = New dsTestCon()
    adapter.Fill(ds.tblTest)

    MsgBox(ds.tblTest.Rows(0).Item(1))