通过绑定源数据库连接导致SQL连接错误

时间:2012-08-23 10:09:23

标签: sql-server vb.net data-binding database-connection

这是我的连接代码:

Imports System.Data.SqlClient
Public Class frm_edit_patient
    Dim con As New SqlConnection
    Private Sub frm_edit_patient_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
        frm_dashboard.Enabled = True
        Me.Hide()
    End Sub
    Private Sub frm_edit_patient_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        'TODO: This line of code loads data into the 'Database1DataSet1.patients' table. You can move, or remove it, as needed.
        Me.PatientsTableAdapter.Fill(Me.Database1DataSet1.patients)
        frm_dashboard.Enabled = False
        Try
            Dim myConString As String = My.Settings.Database1ConnectionString
            con.ConnectionString = myConString
            con.Open()
            con.Close()
            MessageBox.Show("connected")
        Catch ex As Exception
            MessageBox.Show(ex.ToString)
        End Try
    End Sub

    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
        frm_dashboard.Enabled = True
        Me.Hide()
    End Sub
End Class

但是这给了我错误:

enter image description here

我正在使用数据库连接的绑定源....

1 个答案:

答案 0 :(得分:1)

Database.sdf是一个Sql Compact数据库文件,而不是Sql Server目录名。

您需要使用SqlCeConnection而不是SqlConnection

因此你需要声明

Dim con As New SqlCeConnection

还记得你需要一个对程序集System.Data.SqlServerCe的引用 (在System.Data.SqlServerCe.dll中)