locked
SQL connection - web.config RRS feed

  • Question

  • User-816021401 posted

    Hello,

    I have read most of the threads with this problem, and was searching for a solution everywhere but nothing helped, please just post a solution that I could try to implement. The connection string that i used in Visual Studio 2010 also does not work.

    So, I am using Visual Studio Express 2012, the web.confing string is the following (the latest that I have tryed to use):

    <connectionStrings>
    <add name="myConnectionString" connectionString="Data Source=|DataDirectory|\Database.mdf;Initial Catalog=MyDataBaseName;Integrated Security=True"
    providerName="System.Data.SqlClient" />
    </connectionStrings>

    Code:

    string sqlQuery = "SELECT ime FROM Korisnik";
    string connectionString = System.Configuration.ConfigurationManager.ConnectionStrings["myConnectionString"].ConnectionString;
    SqlConnection sqlcon = new SqlConnection(connectionString);
    sqlcon.Open();

    And finally, the latest error that was thrown:

    A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: SQL Network Interfaces, error: 26 - Error Locating Server/Instance Specified)

    It's very frustrating, thank you people! {#emotions_dlg.kiss}

     



     

    Monday, February 4, 2013 2:53 PM

Answers

  • User-816021401 posted

    Thank you anilreddy128, I have found a way! Cool

    Web.config:

    <connectionStrings>
    <add name="sqlString" connectionString="Data Source=(LocalDB)\v11.0;AttachDbFilename=|DataDirectory|\Database.mdf;Integrated Security=True"
    providerName="System.Data.SqlClient" />
    </connectionStrings>

    Code:

    string dbString = ConfigurationManager.ConnectionStrings["sqlString"].ConnectionString;
    SqlConnection dbConn = new SqlConnection(dbString);
    dbConn.Open();
    dbConn.Close();

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, February 5, 2013 12:24 PM

All replies

  • User-344705538 posted

    Provider seems to be a problem here. Try changing it to OLEDB and connect.

    Provider=Microsoft.Jet.OLEDB.4.0

    for access OleDbConnection should be used, not SqlConnection..........


    http://www.connectionstrings.com/access


    Monday, February 4, 2013 3:57 PM
  • User-816021401 posted

    Thank you anilreddy128, I have found a way! Cool

    Web.config:

    <connectionStrings>
    <add name="sqlString" connectionString="Data Source=(LocalDB)\v11.0;AttachDbFilename=|DataDirectory|\Database.mdf;Integrated Security=True"
    providerName="System.Data.SqlClient" />
    </connectionStrings>

    Code:

    string dbString = ConfigurationManager.ConnectionStrings["sqlString"].ConnectionString;
    SqlConnection dbConn = new SqlConnection(dbString);
    dbConn.Open();
    dbConn.Close();

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, February 5, 2013 12:24 PM
  • User-344705538 posted

    wow... Thats cool.........

    Tuesday, February 5, 2013 2:07 PM