Database Connectivity in asp.net for Social Networking Website
Database Connectivity for Social Networking Website
- Database Connectivity is very important part of any web application as we needs to insert,update,select and delete data.
- There has to be interaction between front end and the back end.
- This interaction is achieved by database connectivity.In our case the front end is Asp web forms and the backend is SQL Server 2008 or 2008
Database.cs C# Code:
Open Database.cs which we have created in AppCode Folder.In Database.cs File you can add the following piece of code
For Connectivity we have made a Public Static Class with the name Database.
When we make Public class it can be accessed from anywhere in the project.when we make static class we dont need to create object of that class to call its methods.
Inside Public Static Class Database we have create four static methods for four opertions like select,insert,delete and update.
This are also static methods so we can call them directly.All the four methods are having a parameter as string which is nothing but the database query.
This Methods will receive the query and perform database connection using the connection string and then return the result in the form of datatable or will not return anything means void.
To get the connection string from the webconfig file we have made use of WebConfigurationManager.
We have named the connection string as HearBeat.You can give any name which you desire but it should be same both in Database.cs file and web.config file otherwise your database connectivity will give unwanted errors
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.SqlClient;
using System.Data;
using System.Web.Configuration;
public static class Database
{
public static string connString = WebConfigurationManager.ConnectionStrings["HeartBeat"].ConnectionString;
public static DataTable GetData(string query)
{
DataTable dt = new DataTable();
SqlConnection conn = new SqlConnection(connString);
SqlDataAdapter sda = new SqlDataAdapter(query, conn);
conn.Open();
sda.Fill(dt);
conn.Close();
return dt;
}
public static void UpdateData(string query)
{
SqlConnection conn = new SqlConnection(connString);
SqlCommand cmd = new SqlCommand(query, conn);
conn.Open();
cmd.ExecuteNonQuery();
conn.Close();
}
public static void DeleteData(string query)
{
SqlConnection conn = new SqlConnection(connString);
SqlCommand cmd = new SqlCommand(query, conn);
conn.Open();
cmd.ExecuteNonQuery();
conn.Close();
}
public static void InsertData(string query)
{
SqlConnection conn = new SqlConnection(connString);
SqlCommand cmd = new SqlCommand(query, conn);
conn.Open();
cmd.ExecuteNonQuery();
conn.Close();
}
}
Web.config Connection String Code:
Open Web.config file and then add the connection string shown below.You have to change the data source name and put your sql server providers name.
You can also change the database name from HearBeatDB if you made a database with some other name.
You have to add the Connection String section inside the Configuration Section of web config file
<configuration>
<connectionStrings>
<add name="HeartBeat" connectionString="Data Source=YourSQLServerSourceName;
Initial Catalog=HeartBeatDB;Integrated Security=True;"
providerName="System.Data.SqlClient"/>
</connectionStrings>
</configuration>
where to find for SQL server source name
ReplyDeletein query we should write select update delete querys r8
ReplyDelete