如何将我的.mdf数据库连接到vb.net?

时间:2021-04-29 13:55:40

I'm trying to connect my local .mdf file to vb.net and this argument exception pops up "Keyword not supported. Parameter name: attachdbfilename"

我正在尝试将我的本地.mdf文件连接到vb.net,并且此参数异常弹出“不支持关键字。参数名称:attachdbfilename”

here's my code

这是我的代码

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 

1 个解决方案

#1


2  

The problem is you are using MySqlConnection, and that only must be used with MySql databases. You must use SqlConnection:

问题是您正在使用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 

#1


2  

The problem is you are using MySqlConnection, and that only must be used with MySql databases. You must use SqlConnection:

问题是您正在使用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