Knowledge Walls
Gopal Rao
Mumbai, Maharashtra, India
Passcode:
Create database connection in asp.net using c#
5581 Views
Introduction 
In this code i explained about how to make connection to database via c# code.
Code
	using System;
	using System.Collections.Generic;
	using System.Linq;
	using System.Text;
	using System.Data.SqlClient;//need this reference to connect database
	using System.Data;//need this reference to connect database
	namespace DAL
	{
	   public class DalViewTripDetails
	   {
	        //To refer the connection database.The below one for refer the server database.
	       // SqlConnection conn = new SqlConnection("data source=172.25.192.72;database=DB04H12;user id=pj04h12;password=tcshyd");
	       string conn = @"Data Source=LAKSHANYA\SQLEXPRESS; Initial Catalog=master;Integrated Security=True";
	       //To refer the local windows database
	       try
	           {
	               conn.Open();//open the connection
	               SqlCommand cmd = new SqlCommand("select * from Trip_Details where userId="+user+"order by Journey_Id DESC",
	conn);//using query selecting fields from DB
	               cmd.ExecuteNonQuery();//execute the query
	           }
	           catch (Exception e)
	           {
	               throw e;
	           }
	           finally
	           {
	               conn.Close();//close the connection
	           }
	   }
	}
Previous Topics
Previous lessons of current book.
Best Lessons of "Asp.net/Ado.net/C#.net(Dotnet)"
Top lessons which are viewed more times.
  Copyright © 2014 Knowledge walls, All rights reserved
KnowledgeWalls
keep your tutorials and learnings with KnowledgeWalls. Don't lose your learnings hereafter. Save and revise it whenever required.
Click here for more details