2017-02-09 73 views
0

我想我的本地.mdf文件連接到vb.net這個參數異常彈出「關鍵字不支持參數名:attachdbfilename」如何將我的.mdf數據庫連接到vb.net?

這裏是我的代碼

Imports MySql.Data.MySqlClient 

Public Class Form1 

Dim con As MySqlConnection 

Private Sub PictureBox1_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PictureBox1.Click 
    con = New MySqlConnection 
    con.ConnectionString = "Data Source=.\SQLEXPRESS;AttachDbFilename=C:\Users\almond\Desktop\TICKETING SYSTEM\TICKETING SYSTEM\Database1.mdf;Integrated Security=True;User Instance=True" 
    Try 
     con.Open() 
     MessageBox.Show("Connected!") 
     con.Close() 
    Catch ex As MySqlException 
     MessageBox.Show(ex.Message) 
    Finally 
     con.Dispose() 
    End Try 

End Sub 
+0

您將需要Microsoft SQL的SqlConnection類。 MySQL是不同的。 –

回答

2

的問題是您正在使用MySqlConnection,並且只能與MySql數據庫一起使用。您必須使用SqlConnection

Imports System.Data.SqlClient; 

Public Class Form1 

Dim con As SqlConnection 

Private Sub PictureBox1_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PictureBox1.Click 
    con = New SqlConnection 
    con.ConnectionString = "Data Source=.\SQLEXPRESS;AttachDbFilename=C:\Users\almond\Desktop\TICKETING SYSTEM\TICKETING SYSTEM\Database1.mdf;Integrated Security=True;User Instance=True" 
    Try 
     con.Open() 
     MessageBox.Show("Connected!") 
     con.Close() 
    Catch ex As SqlException 
     MessageBox.Show(ex.Message) 
    Finally 
     con.Dispose() 
    End Try 

End Sub 
+0

非常感謝!這從來沒有超過我的想法。 –

相關問題