Source code for database connection using ado.net
Insert the data
Delete the data
Retrieve the data
Loading
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.
Priya LingePosted Aug 29, 2011, 7:14 AM
using System;
using System.Data;
using System.Data.SqlClient;
//Sqlconnection
Public class example();Initial Catalog=pubs;Data Source=(local)";
{
string sConnectionString =
"User ID=
SqlConnection objConn = new SqlConnection(sConnectionString);
Public void InsertData()
{
objConn.Open();
string sSQL = "INSERT INTO Employee " +
"(emp_id, fname, hire_date) " +
"VALUES ('MSD12923F', 'Duncan','2001-01-01')";
SqlCommand objCmd = new SqlCommand(sSQL,objConn);
try
{
objCmd.ExecuteNonQuery();
}
catch (System.Exception e)
{
Console.WriteLine(e.Message);
}
Console.WriteLine("Record Added");
}
Public void Deleterecord()
{
objConn.Open();
string sSQL = "DELETE FROM Employee WHERE emp_id = @emp_id";
SqlCommand objCmd = new SqlCommand(sSQL,objConn);
objCmd.Parameters.Add("@emp_id", SqlDbType.Char, 9);
objCmd.Parameters["@emp_id"].Value = "MSD12923F";
try
{
objCmd.ExecuteNonQuery();
}
catch (System.Exception e)
{
Console.WriteLine(e.Message);
}
Console.WriteLine("Record Deleted");
}
Public void RetriveData()
{
objConn.Open();
string sSQL = Select Ename FROM Employee ";
SqlCommand objCmd = new SqlCommand(sSQL,objConn);
SqldataReader dr=objCmd.ExcuteReader();
}
}
Hope this will help you.
Thanks.