DiigIT | IT Community
No Profile Image
Welcome Guest
New User? Register | Login

How to make database connection

By: Admin | 10 Feb 2010 4:02 pm

Hello,

I want to knw ..how to make database connection with ur program..... .help me out...
plz also give slide view of it,......... .......

--
Regards

Comments

Hi.
 
I want to give to u some basic knowledge about Data Access in .net.
First of all if you want to make a connecition from database then ,
1.Open Web.Config file which is placed in your project.
2.then make connecition string like that,
You will see there a tag something like that:
<connectionString />ok,now change it .
<connectionString>
<add name="Connecitonstr ingname" connectinString= "Data Source=yourserverna me/sqlexpress; Initial Catalog=Yourdatabas ename;Integrated Security=true" Providername= "System.Data. SqlClient"></ connectionString>
 
 
3.Now you have made a connection string named "ConnectinStringnam e" , you will use it to connect database
 
There are two ways to Connect:
1.Connect through DataGrid,Detail, Form View.
2.Make Custom inserting or retrieving
 
 
1.Connect Through DataGrid:
 
its a simple method just drag and drop data grid view from tool bar and right click on it and chose new data source ,make connection through wizard , its very easy ,
 
2. Make Custom Retrieving :
 
Add namespace :
 
 using System.Data. SqlClient;
 
after that now on event handler
for example you want to run you code on form load then
Protected Void Page_Load(object sender  Event Args e)
{
  Sqlconnection con = new SqlConnection( ConfigurationMan ager.ConnectionS tring["Yourconec tionstringname" ].ConnectionStri ng);
 
SqlCommand com = new Sqlcommand(" select * from tablename",con) ;
com.CommandType= CommandType. text;
con.open();
Sqldatareader dr = com.ExecuteReader( );
while (dr.read())
{
 label1.Text = dr["Columnname" ].ToString( );
}
dr.close();
con.close();
}
 
Study this code is retrieving the data from database using data reader and print the text from columname to label1 , it will display on the screen ,
 
 
Regards..

By: Admin | 10 Feb 2010
You can go to web config file and do the following here -->cn is the name of connection server's name is tehamb6(the name of sql server) database-->ambala15(that u want to give) if user id and password are there then u have to give it .
By: Admin | 10 Feb 2010

Leave a comment

Enter the text in the image
img
Can't read?
Type the characters you see in the picture below.


Close Move