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))
{
//NOTE:Scalar functions can take multiple values and always returns single value
//string query = "select max(salary) from employee_tbl";
//string query = "select min(salary) from employee_tbl";
//string query = "select count(salary) from employee_tbl";
string query = "select sum(salary) from employee_tbl";
SqlCommand cmd = new SqlCommand(query, con);
con.Open();
int a = (int)cmd.ExecuteScalar();
Console.WriteLine(a);
}
}
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))
// {
// Console.WriteLine("Employee ID : ");
// string id = Console.ReadLine();
// string query = "delete from employee_tbl where id=@id";
// SqlCommand cmd = new SqlCommand(query, con);
// cmd.Parameters.AddWithValue("@id", id);
// con.Open();
// int a = cmd.ExecuteNonQuery();
// if (a > 0)
// {
// Console.WriteLine("Data has been Deleted Successfully....");
// }
// else
// {
// Console.WriteLine("Data Deletion Failed....");
// }
// }
// }
// 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))
// {
// Console.WriteLine("Employee ID : ");
// string id = Console.ReadLine();
// Console.WriteLine("Employee Name : ");
// string name = Console.ReadLine();
// Console.WriteLine("Employee Gender : ");
// string gender = Console.ReadLine();
// Console.WriteLine("Employee Age : ");
// string age = Console.ReadLine();
// Console.WriteLine("Employee Salary : ");
// string salary = Console.ReadLine();
// Console.WriteLine("Employee City : ");
// string city = Console.ReadLine();
// string query = "update employee_tbl set name=@name,gender=@gender,age=@age,salary=@salary,city=@city where id=@id";
// SqlCommand cmd = new SqlCommand(query, con);
// cmd.Parameters.AddWithValue("@id", id);
// cmd.Parameters.AddWithValue("@name", name);
// cmd.Parameters.AddWithValue("@gender", gender);
// cmd.Parameters.AddWithValue("@age", age);
// cmd.Parameters.AddWithValue("@salary", salary);
// cmd.Parameters.AddWithValue("@city", city);
// con.Open();
// int a = cmd.ExecuteNonQuery();
// if (a > 0)
// {
// Console.WriteLine("Data has been Updated Successfully....");
// }
// else
// {
// Console.WriteLine("Data Updation Failed....");
// }
// }
// }
// 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))
// {
// Console.WriteLine("Employee Name : ");
// string name = Console.ReadLine();
// Console.WriteLine("Employee Gender : ");
// string gender = Console.ReadLine();
// Console.WriteLine("Employee Age : ");
// string age = Console.ReadLine();
// Console.WriteLine("Employee Salary : ");
// string salary = Console.ReadLine();
// Console.WriteLine("Employee City : ");
// string city = Console.ReadLine();
// string query = "insert into employee_tbl values(@name,@gender,@age,@salary,@city)";
// SqlCommand cmd = new SqlCommand(query, con);
// cmd.Parameters.AddWithValue("@name", name);
// cmd.Parameters.AddWithValue("@gender", gender);
// cmd.Parameters.AddWithValue("@age", age);
// cmd.Parameters.AddWithValue("@salary", salary);
// cmd.Parameters.AddWithValue("@city", city);
// con.Open();
// int a = cmd.ExecuteNonQuery();
// if (a > 0)
// {
// Console.WriteLine("Data has been insertd Successfully....");
// }
// else
// {
// Console.WriteLine("Data Insertion Failed....");
// }
// }
// }
// 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))
// {
// //1st way
// //string query = "spGetEmployees";
// //SqlCommand cmd = new SqlCommand(query,con);
// //cmd.CommandType = CommandType.StoredProcedure;
// //2nd way
// //string query = "select * from employee_tbl";
// ////SqlCommand cmd = new SqlCommand(query,con);
// //SqlCommand cmd = new SqlCommand();
// //cmd.CommandText = query;
// //cmd.Connection = 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)
Comments
Post a Comment