in this article, i will show you how to connect database in asp.net web pages with sql server.
step : 1 configure the connection string in the web.config file:
in configuration tage add this line of code,
<connectionStrings>
<add name="TDB" connectionString="data source=.;Initial Catalog=TESTDB;Integrated Security=True;" providerName="System.Data.SqlClient" />
</connectionStrings>
step : 2 install nuget package
Microsoft.AspNet.WebPages.WebData
step : 3 create cshtml view
@using WebMatrix.Data;
@{
var db = Database.Open("TDB"); // connection string name from web.config
var query = "SELECT * FROM Employee";
}
<html>
<body>
<h1>Employee Details</h1>
<table border="1" width="100%">
<tr>
<th>Id</th>
<th>Name</th>
<th>Salary</th>
</tr>
@foreach (var data in db.Query(query))
{
<tr>
<td>@data.id</td>
<td>@data.name</td>
<td>@data.salary</td>
</tr>
}
</table>
</body>
</html>
in configuration tage add this line of code,
<connectionStrings>
<add name="TDB" connectionString="data source=.;Initial Catalog=TESTDB;Integrated Security=True;" providerName="System.Data.SqlClient" />
</connectionStrings>
step : 2 install nuget package
Microsoft.AspNet.WebPages.WebData
step : 3 create cshtml view
@using WebMatrix.Data;
@{
var db = Database.Open("TDB"); // connection string name from web.config
var query = "SELECT * FROM Employee";
}
<html>
<body>
<h1>Employee Details</h1>
<table border="1" width="100%">
<tr>
<th>Id</th>
<th>Name</th>
<th>Salary</th>
</tr>
@foreach (var data in db.Query(query))
{
<tr>
<td>@data.id</td>
<td>@data.name</td>
<td>@data.salary</td>
</tr>
}
</table>
</body>
</html>
0 Comments