Source code for database connection using ado.net
Insert the data
Delete the data
Retrieve the data
ADO.NET Connection using C#
1 Answer
Know the answer? Post it. Somebody with the same question will find it here.
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Priya LingeRank #785 · 2.9K reputationPosted Aug 29, 2011, 3:14 AM EDT
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.