Unable to execute ConnectionString in MS visual St

2019-08-30 09:32发布

I've been stuck with this for the past week and would be very glad if you guys could help.

I'm working on vb with MS visual studio 2010. I currently have a database under a asp.net folder named "app_data".

I'm trying to execute a connectionString command via web.config file.

Web Config:

<connectionStrings>
    <add name="mysqlcon" connectionString="Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\public.mdf;Integrated Security=True;User Instance=True"
      providerName="System.Data.SqlClient" />
  </connectionStrings>

Main code:

 Dim a As String = ConfigurationManager.ConnectionStrings("mysqlcon").ConnectionString
        Dim conn As New MySqlConnection(a)



        Try


            conn.Open()
            MsgBox("good")

            conn.Close()
        Catch ex As MySqlException
            MsgBox(ex.Message)
        Finally
            conn.Dispose()

        End Try

I'm getting the following error(line 10 is being highlighted):

Server Error in '/MP' Application.

Keyword not supported.

Parameter name: attachdbfilename


Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.ArgumentException: Keyword not supported.
Parameter name: attachdbfilename

Source Error:


Line 8:      Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
Line 9:          Dim a As String = ConfigurationManager.ConnectionStrings("mysqlcon").ConnectionString
Line 10:         Dim conn As New MySqlConnection(a)
Line 11: 
Line 12: 



Stack Trace:


[ArgumentException: Keyword not supported.
Parameter name: attachdbfilename]
   MySql.Data.MySqlClient.MySqlConnectionStringBuilder.ValidateKeyword(String keyword) +108
   MySql.Data.MySqlClient.MySqlConnectionStringBuilder.set_Item(String keyword, Object value) +18
   System.Data.Common.DbConnectionStringBuilder.set_ConnectionString(String value) +185
   MySql.Data.MySqlClient.MySqlConnection.set_ConnectionString(String value) +289
   MySql.Data.MySqlClient.MySqlConnection..ctor(String connectionString) +26
   test.Button1_Click(Object sender, EventArgs e) in C:\Users\God\Desktop\VB\MP\test.aspx.vb:10
   System.Web.UI.WebControls.Button.OnClick(EventArgs e) +118
   System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument) +112
   System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +10
   System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +13
   System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +36
   System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +5563

Would be glad if you guys can help.

2条回答
ら.Afraid
2楼-- · 2019-08-30 10:10

You are trying to connect using a MySQL connection object:

Dim conn As New MySqlConnection(a)

with a Microsoft SQL Server connection string:

<add name="mysqlcon" connectionString="Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\public.mdf;Integrated Security=True;User Instance=True" providerName="System.Data.SqlClient" />

They just don't match. If you want to connect to a MySQL database, change the connection string accordingly.

查看更多
SAY GOODBYE
3楼-- · 2019-08-30 10:26

user2072778, try this one, it might work:

<add name="mysqlcon" connectionString="Data Source=|DataDirectory|public.mdf" providerName="System.Data.SqlClient" />

And this is a good page for searching this theme: http://www.connectionstrings.com/sql-server-2008

查看更多
登录 后发表回答