SqlCommand Class ADO.Net | ExecuteNonQuery | ExecuteReader | ExecuteScalar
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
namespace SQLConnectionADO
{
internal class Program
{
static void Main(string[] args)
{
Program.Connection();
Console.ReadLine();
}
static void Connection()
{
string cs = ConfigurationManager.ConnectionStrings["dbcs"].ConnectionString;
SqlConnection con = null;
try
{
using (con = new SqlConnection(cs))
{
string query = "select * from employee_tbl";
SqlCommand cmd = new SqlCommand(query,con);
con.Open();
SqlDataReader dr = cmd.ExecuteReader();
while(dr.Read())
{
Console.WriteLine("Id = " + dr["id"] + ", Name = " + dr["name"] + ", Gender = " + dr["gender"] + ", Age = " + dr["age"] + ", Salary = " + dr["salary"] + ", City = " + dr["city"]);
}
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
finally
{
con.Close();
}
}
//static void Connection()
//{
// string cs = ConfigurationManager.ConnectionStrings["dbcs"].ConnectionString;
// SqlConnection con = null;
// try
// {
// using (con = new SqlConnection(cs))
// {
// con.Open();
// if (con.State == ConnectionState.Open)
// {
// Console.WriteLine("Connection has been Created Successfully !!");
// }
// }
// }
// catch (SqlException ex)
// {
// Console.WriteLine(ex.Message);
// }
// finally
// {
// con.Close();
// }
//}
//static void Connection()
//{
// string cs = "Data Source=HEER\\SQLEXPRESS;Initial Catalog=ado_d;Integrated Security=true;";
// SqlConnection con = null;
// try
// {
// using (con = new SqlConnection(cs))
// {
// con.Open();
// if (con.State == ConnectionState.Open)
// {
// Console.WriteLine("Connection has been Created Successfully !!");
// }
// }
// }
// catch (SqlException ex)
// {
// Console.WriteLine(ex.Message);
// }
// finally
// {
// con.Close();
// }
// //using(SqlConnection con = new SqlConnection(cs))
// //{
// // con.Open();
// // if (con.State == ConnectionState.Open)
// // {
// // Console.WriteLine("Connection has been created successfully.");
// // }
// //}
// //SqlConnection con = new SqlConnection(cs);
// //try
// //{
// // con.Open();
// // if (con.State == ConnectionState.Open)
// // {
// // Console.WriteLine("Connection has been created successfully.");
// // }
// //}
// //catch (SqlException ex)
// //{
// // Console.WriteLine(ex.Message);
// //}
// //finally
// //{
// // con.Close();
// //}
//}
}
}
.png)
.png)
.png)
.png)
.png)
.png)
.png)
.png)
.png)
.png)
.png)
.png)
.png)
.png)
.png)
.png)
.png)
Comments
Post a Comment