C# : Tutorial of Asp.net with c# and example

Example and code about windows application, web application, SQL, C#, WPF etc which is helpful to developer and programer.

Search This Blog

SQL Connection: Very Easy Code For SQL Connection by C# and Asp.Net

♠ Posted by uvesh in at Sunday, August 03, 2014


How To make sql connection using C# Asp.net

The first of all need to do when interacting with a data base is to create a connection. It Microsoft SQL Server is a relational database management system developed by Microsoft.
Manages all of the low level logic associated with the specific database protocols. In code is instantiate the connection object, open the connection, and then close the connection when you are done. Because of the way that other classes in ADO.NET are built, sometimes you don't even have to do that much work.

Although working with connections is very easy, you need to understand connections in order to make the right decisions when coding your data access routines. Understand that a connection is a valuable resource. 

Following code write down in code behind page (aspx.cs page) For import Class Library of .net

using System.Data.SqlClient;


Following code write down in code behind page (aspx.cs page) For make connection 


SqlConnection myConnection = new SqlConnection("user id=username;" + 
                                       "password=password;server=serverurl;" + 
                                       "Trusted_Connection=yes;" + 
                                       "database=database; " + 

                                       "connection timeout=30");



This is the last part of getting connected and is simply executed by the following (remember to make sure your connection has a connection string first):

SqlConnection.Open()

This code put in Try Catch because this function may be throw error so try catch is usefull to handle error.

try
{
    myConnection.Open();
}
catch(Exception e)
{

    Console.WriteLine(e.ToString());
}



If our post is useful please share and comment on our post for more post and detail Visit :

http://aspdotnethelpmaster.blogspot.in/

0 comments:

Post a Comment