What is the use of the Connection object
brief describe about connection object and how it work.
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
Shivanand ArurPosted Sep 23, 2012, 5:42 AM
So you would like to show that information stored in your database on the website you have created. In that case, you will have to connect to the SQL Database to fetch data from it. Other than fetching the data, you can also insert, update and delete content from the database. But the most important thing is the connection with the database.
.Net provides us classes to connect to various database. For that you need to use the System.Data and System.Data.SqlClient(for SQL Database) namespaces.
Once you add these references in your code, you can use various classes available in these packages.
Definition - A Connection object is basically used to connect to a particular database to do any kind of transactions with it...
Check out the example given below...
using System;
using System.Data;
using System.Data.SqlClient;
namespace UnderstandingConnectionObject
{
class Program
{
static void Main(string[] args) {
SqlConnection con = new SqlConnection();
con.ConnectionString = "Your Connection String Comes here";
SqlCommand cmd = new SqlCommand("Select * from Tablename", con);
int Value = cmd.ExecuteNonQuery();
if (Value != 0)
{
Console.WriteLine("Query Executed Successfully");
}
else
{
Console.WriteLine("Query Execution Failed");
}
}
}
// Creates a SQL Connection Object. Its Constructor accepts one or zero parameters
SqlConnection con = new SqlConnection();
// Specifying the Connection String to Connect to the Database.
con.ConnectionString = "Your Connection String Comes here";
This is just an example, which will explain you how to use Connection Object. Hope this helps you. Feel free to ask any questions if you have any doubt.
Check this site for understanding it in a better way...
http://msdn.microsoft.com/en-us/library/ms254507.aspx
PLEASE MARK THE ANSWER AS ACCEPTED IF YOU FEEL IT HELPED YOU!!!
Thank you.