locked
Reading a ConnectionString to Access DB RRS feed

  • Question

  • User-2033772850 posted

    Hi, I would like to implement a ConnectionString and I'm not getting the code right.  I'm using vb and connecting to an access database.  I've used a bunch of variations of

    ConfigurationManager.ConnectionStrings("Conectionstring").ConnectionStrings

    but I am definitely not getting it right, as I keep getting a variety of error messages.  Any advice is greatly appreciated!

    In my web config I have:

    <connectionStrings>
        <add name="MyConnectionString" connectionString="Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:...MyDatabase.accdb" providerName="System.Data.OleDb" />
      </connectionStrings>


    Then in my page I have:

    Dim sql, dbconn, dbcomm, dbread
                   
    dbconn = New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\...MyDatabase")
    dbconn.Open()
    sql = "SELECT * From table"
    dbcomm = New OleDbCommand(sql, dbconn)
    dbread = dbcomm.ExecuteReader()
                
    results.DataSource = dbread
    results.DataBind()
    dbread.Close()
    dbconn.Close()



    Tuesday, March 26, 2013 11:30 AM

Answers

  • User1938476581 posted

    Hi,

    I'm sorry for the wrong post.  Using OledbConnection should work like this

      Dim connectionString As String
      Dim connection As OleDbConnection
      connectionString = ConfigurationManager.ConnectionStrings("MyConnectionString").ToString
      connection = New OleDbConnection(connectionString)
      connection.Open() 
    

    For detailed information, you can refer here

    http://asp.net-informations.com/data-providers/asp-oledb-connection.htm

    Hope it can help you.

     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, March 28, 2013 10:30 PM

All replies

  • User197322208 posted

    <add name="MyConnectionString" 

    ConfigurationManager.ConnectionStrings("MyConnectionString").

     

    Tuesday, March 26, 2013 11:44 AM
  • User-2033772850 posted

    Hi, thanks for your quick response.  I'm afraid I'm not sure exactly how to implement it, though.  I tried the following and got the error message "System.MissingMemberException: Public member 'Open' on type 'ConnectionStringSettings' not found."

    dbconn = ConfigurationManager.ConnectionStrings("MyConnectionString")
    dbconn.Open()



    Tuesday, March 26, 2013 12:20 PM
  • User197322208 posted

    use a sqlconnection

    Tuesday, March 26, 2013 9:15 PM
  • User-2033772850 posted

    Thanks, but I am connecting to access database.  I thought I couldn't use a sqlconnection since it is for SQL databases?  

    Wednesday, March 27, 2013 7:53 AM
  • User1938476581 posted

    Hi,

    you should use it like this

    dbconn = ConfigurationManager.ConnectionStrings("MyConnectionString").ConnectionString
    dbconn.Open()

    Thursday, March 28, 2013 2:22 AM
  • User-2033772850 posted

    Hi, thanks so much for your response.  I don't know why I can't get this!  I am now getting a message "System.MissingMemberException: Public member 'Open' on type 'String' not found." I've tried to Dim dbconn as something other than string (either OleDBConnection or AccessDataSource) and also get errors with those.  There must be some small thing I am missing?


    sql = "SELECT * ...."
                    
    
    Dim dbconn, dbcomm, dbread
    dbconn = ConfigurationManager.ConnectionStrings("MyConnectionString").ConnectionString
    dbconn.Open()
                
                    
    dbcomm = New OleDbCommand(sql, dbconn)
    dbread = dbcomm.ExecuteReader()
    results.DataSource = dbread
    results.DataBind()
    
    dbread.Close()
    dbconn.Close()



    Thursday, March 28, 2013 8:35 AM
  • User1938476581 posted

    Hi,

    I'm sorry for the wrong post.  Using OledbConnection should work like this

      Dim connectionString As String
      Dim connection As OleDbConnection
      connectionString = ConfigurationManager.ConnectionStrings("MyConnectionString").ToString
      connection = New OleDbConnection(connectionString)
      connection.Open() 
    

    For detailed information, you can refer here

    http://asp.net-informations.com/data-providers/asp-oledb-connection.htm

    Hope it can help you.

     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, March 28, 2013 10:30 PM
  • User-134477160 posted

    Pengzhen Song - MSFT

    connectionString = ConfigurationManager.ConnectionStrings("MyConnectionString").ToString

    You can also use this:

    connectionString = ConfigurationManager.ConnectionStrings("MyConnectionString").ConnectionString;
    Thursday, March 28, 2013 10:43 PM
  • User-2033772850 posted

    It is working now!  Thank you!!!!

    Friday, March 29, 2013 12:59 PM