Error Accessing Database from Web Pages
source link: https://www.codesd.com/item/error-accessing-database-from-web-pages.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
Error Accessing Database from Web Pages
I am trying to access database from my Web Page in VS2013. It's giving me the error
An exception of type 'System.Data.SqlClient.SqlException' occurred in System.Data.dll but was not handled in user code
Additional information: 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)"
My Code:
@{
var db = Database.Open("Rohit");
var selectQueryString = "SELECT * FROM Sample";
}
<html>
<body>
<h1>Small Bakery Products</h1>
<table>
<tr>
<th>Id</th>
<th>Name</th>
</tr>
@foreach (var row in db.Query(selectQueryString))
{
<tr>
<td>@row.Id</td>
<td>@row.Name</td>
</tr>
}
</table>
It is giving error on the @foreach
line.
The Database.Open(name
) method will connect to a database in two steps:
First, it searches the application's App_Data
folder for a database that matches the name parameter without the file-name extension.
If no file is found, it looks for a "connection string
" in the application's Web.config file.
(A connection string contains information about how to connect to a database. It can include a file path, or the name of an SQL database, with full user name and password)
This two-step search makes it possible to test the application with a local database, and run the application on a web host using a connection string.
Now consider these steps and try to Connect to your Database.
Recommend
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK