0

So I am trying to go through some guides on using asp.net but I have had this issue with using databases I will always get this error:

System.Data.SqlClient.SqlException: '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)'

This is my code I have no clue what is wrong with it at all the names of the databases and tables match up I am using VS 2019.

@using WebMatrix.Data;
@{
    var db = Database.Open("Database1");
    var selectQueryString = "SELECT * FROM Table ORDER BY FirstName";
}

<!DOCTYPE html>
<html>

<head>
    <title>Customers List</title>
    <style>
        table, th, td {
            border: solid 1px #bbbbbb;
            border-collapse: collapse;
            padding: 2px;
        }
    </style>
</head>

<body>
    <h1>Customers List</h1>
    <table>
        <thead>
            <tr>
                <th>Id</th>
                <th>First Name</th>
                <th>Last Name</th>
                <th>Address</th>
            </tr>
        </thead>

        <tbody>
            @foreach (var row in db.Query(selectQueryString))
            {
                <tr>
                    <td>@row.ID</td>
                    <td>@row.FirstName</td>
                    <td>@row.LastName</td>
                    <td>@row.Address</td>
                </tr>
            }
        </tbody>
    </table>

</body>
</html>
Simply Ged
  • 8,250
  • 11
  • 32
  • 40
  • Does `"Database1"` refer to a database file in the project, or a connection string in the config? The error is telling you that it can't connect to that database, so what are the details for the database? – David Nov 25 '19 at 00:00
  • I am not too sure what that means I'm very sorry, new to this. – Zachabossaloler Plays Nov 25 '19 at 00:13
  • I guess to simplify... What database are you trying to connect to? – David Nov 25 '19 at 00:17
  • According to the [documentation](https://learn.microsoft.com/en-us/dotnet/api/webmatrix.data.database.open?view=aspnet-webpages-3.2) `The name associated with the database to open. name can specify an .sdf or .mdf database file that is in the App_Data folder. (Do not include the file-name extension.) Alternatively, name can specify the name of a connection string in the Web.config file.` You need to check that either `Database1` is a file in your `App_Data` folder or you have a connection string called `Database1` defined in your web.config – Simply Ged Nov 25 '19 at 00:22

1 Answers1

1

The documentation says it all. The line

var db = Database.Open("Database1");

refers to either an access database (.mdb) in the app_data folder, or more usually the name of a connection string entry in the web.config file. The connection string entry contains the full connection string to the database. A connection string (eg, for Sql Server) entry in the web.config file looks like this:

<connectionStrings>
    <add name="Database1" connectionString="data source=Myserver;initial catalog=MyDBName;persist security info=True;user id=someid;password=mypwd;MultipleActiveResultSets=True;App=EntityFramework" providerName="System.Data.SqlClient" />
  </connectionStrings>

Google standard connection string syntax for what ever database you are using. This should get you on your way.

Programnik
  • 1,449
  • 1
  • 9
  • 13