Database connectivity to sql server asp.net
-
Upload
hemant-sankhla -
Category
Education
-
view
83 -
download
5
Transcript of Database connectivity to sql server asp.net
DATABASE CONNECTIVITY
Programming : C#Technology : Asp.netDatabase : MS Sql Server
Author : Hemant Sankhla (Web Developer)
I will teach you how to connect MS Sql Server
Database to an Asp.Net Web Application using
C#. After the successful connection you are able
to perform various operations on database from
your web application like select, insert, update,
delete etc.
Lets check it out and do yourself.
Author : Hemant Sankhla (Web Developer)
First of all Create Database in MS Sql Server
1. Go to Database > Right Click > New Database. And
give a name to your Database.
2. Now Create a table in this database. To do such
select the database > Right click On Table > New Table.
Now enter desired fields and save it using ctrl + s.
Now your database with tables is ready to be a part of
your web application.
Author : Hemant Sankhla (Web Developer)
Create The Connection String (Path to server and
database)
Connection string means the address to the Sql Server and The database
using the security access, or if there is an username and password then
providing these credentials for accessing the server and database.
Now type below line in ‘web.config’ file which you have in your website
directory, search for it in Solution Explorer. If we make connection in
‘web.config’ file then we only have to make it once and we call it where
we want it.
<connectionStrings>
<add name = "con" connectionString="Data Source=.\
sqlexpress ; Initial Catalog=Database; Integrated
Security=true"/>
</connectionStrings>
Author : Hemant Sankhla (Web Developer)
Create The Connection String (Path to server and
database)
name : The name of your connection
ConnectionString : The full path or address to your server and
database
DataSource : Sql Server Name (you can get this in Sql server
management studio. Copy the server name from the connection dialog
box at starting.)
Initial Catalog : Your Database.
Integrated Security : True, use this if you have no username and
password in sql server.
Author : Hemant Sankhla (Web Developer)
Using NameSpaces
Namespace is a collection of classes and methods. In C# various
namespaces are used in different work. Here we want to connect
and use the Sql Server so we have to use appropriate
namespaces for this task. Namespaces are used by the ‘Using
keyword.’
Add these two name spaces.
using System.Data;
using System.Data.SqlClient;
Author : Hemant Sankhla (Web Developer)
Using NameSpaces
Namespace is a collection of classes and methods. In C# various
namespaces are used in different work. Here we want to connect
and use the Sql Server so we have to use appropriate
namespaces for this task. Namespaces are used by the ‘Using
keyword.’
Add these two name spaces.
using System.Data;
using System.Data.SqlClient;
using System.Web.Configuration;
Author : Hemant Sankhla (Web Developer)
The Essential CodeBelow is the essential code for all type of operation.
SqlConnection con = new SqlConnection
(WebConfigurationManager.ConnectionStrings["con"].ToString( ));
con.Open();
SqlCommand cmd = new SqlCommand();
cmd.Connection = con;
Author : Hemant Sankhla (Web Developer)
Understanding The Code
SqlConnection : This class creates a connection object. Con is
the object for connection.
WebConfigurationManager : This class fetch the connection
string from the web.config file.
Con.open( ) : Open the current connection.
SqlCommand : Create the Sql command Query’s object.
Cmd.connection = con : Supply the con object to the cmd
connection.
Author : Hemant Sankhla (Web Developer)
Select All SqlConnection con = new
SqlConnection(WebConfigurationManager.ConnectionStrings["con
"].ToString());
con.Open();
SqlCommand cmd = new SqlCommand();
cmd.Connection = con;
cmd.CommandText = "select * from Table ";
SqlDataAdapter da = new SqlDataAdapter();
DataSet ds = new DataSet();
da.SelectCommand = cmd;
da.Fill(ds);
Author : Hemant Sankhla (Web Developer)
Insert Into SqlConnection con = new
SqlConnection(WebConfigurationManager.ConnectionStrings["con"].ToStri
ng());
con.Open();
SqlCommand cmd = new SqlCommand();
cmd.Connection = con;
cmd.CommandText = "insert into Student (FullName, Marks) values ('" +
txtName.Text + "','" + txtMarks.Text + "')";
int h = cmd.ExecuteNonQuery();
if (h > 0)
{
Response.Write("Inserted Successfully");
}
Author : Hemant Sankhla (Web Developer)
Update Record SqlConnection con = new
SqlConnection(WebConfigurationManager.ConnectionStrings["con"].ToStri
ng());
con.Open();
SqlCommand cmd = new SqlCommand();
cmd.Connection = con;
cmd.CommandText = “update Student SET FullName = '" +
txtName.Text + "', Marks = '" + txtMarks.Text + “’ where ID = ID;
int h = cmd.ExecuteNonQuery();
if (h > 0)
{
Response.Write(“Record Updated Successfully");
}
Author : Hemant Sankhla (Web Developer)
Delete Record
SqlConnection con = new
SqlConnection(WebConfigurationManager.ConnectionStrings["con"].ToStri
ng());
con.Open();
SqlCommand cmd = new SqlCommand();
cmd.Connection = con;
cmd.CommandText = “delete from Student where ID =‘ ” + ID + “ ‘;
int h = cmd.ExecuteNonQuery();
if (h > 0)
{
Response.Write(“Record Deleted Successfully");
}
Author : Hemant Sankhla (Web Developer)
Now you have to practice those codes by hand. If you feel any trouble
comment me
These codes also work well in C# Windows Desktop Application.
Author : Hemant Sankhla (Web Developer)