Thứ Ba, 21 tháng 3, 2017

ADO.NET Demo

using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
 
namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            string connectionString = @"Data Source=USERMIC-42NCNNO\SQLEXPRESS;Initial Catalog=test;User id=sa;Password=7880;";
            SqlConnection con=new SqlConnection();
            con.ConnectionString=connectionString;
            if(con.State!=ConnectionState.Open)
                con.Open();
 
            /*--------------------------------LỆNH SELECT-------------------------------------*/
 
            /*
            SqlCommand cmdSQL = new SqlCommand();
            cmdSQL.Connection = con;
            cmdSQL.CommandText = "Select * from Nhanvien where tuoi>@sTuoi";
            cmdSQL.Parameters.AddWithValue("sTuoi", 27);
            SqlDataReader dr = cmdSQL.ExecuteReader();
            while (dr.Read())
            {
                Console.WriteLine(String.Format("{0} \t | {1} \t | {2} \t | {3}",
                            dr[0], dr[1], dr[2], dr[3]));
            }
            con.Close();
             */
            SqlCommand cmd = new SqlCommand();
            cmd.Connection = con;
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.CommandText="SP_NHANVIEN_SELECT";
            /*
             Store Procedure:
             CREATE PROCEDURE SP_NHANVIEN_SELECT
                @Tuoi INT
             AS
                    SELECT * FROM   NHANVIEN WHERE  TUOI > @Tuoi
             */
            cmd.Parameters.AddWithValue("@tuoi"SqlDbType.Int).Value = 34;
 
            //SqlDataReader dr = cmd.ExecuteReader();
            //while (dr.Read())
            //{
            //    Console.WriteLine(String.Format("{0} \t | {1} \t | {2} \t | {3}",
            //               dr.GetInt32(0), dr["HoTen"], dr[2], dr[3]));
            //}
            //dr.Close();
 
            //int kq = (Int32)cmd.ExecuteScalar();
            //Console.WriteLine(kq);
 
            /*---------------------------------------------------------------------*/
 
            /*--------------------------------LỆNH INSERT-------------------------------------*/
            
            //SqlCommand insertCmd = new SqlCommand();
            //insertCmd.Connection = con;
            //insertCmd.CommandText = @"insert into NhanVien(HoTen,Tuoi,PhongBanId) values(@val1,@val2,@val3)";
            //insertCmd.Parameters.AddWithValue("val1", SqlDbType.NVarChar).Value = "Anh Anh";
            //insertCmd.Parameters.AddWithValue("val2", SqlDbType.Int).Value = 26;
            //insertCmd.Parameters.AddWithValue("val3", SqlDbType.Int).Value = 1;
 
            //int sodong = insertCmd.ExecuteNonQuery();
            //Console.WriteLine(sodong);
 
            /*---------------------------------------------------------------------*/
 
            /*--------------------------------LỆNH UPDATE-------------------------------------*/
 
            //SqlCommand updateCmd = new SqlCommand();
            //updateCmd.Connection = con;
            //updateCmd.CommandText = @"update NhanVien set HoTen=@val1,Tuoi=@val2,PhongBanId=@val3 where nhanvienid=@val4";
            //updateCmd.Parameters.AddWithValue("val1", SqlDbType.NVarChar).Value = "Anh Anh Edit";
            //updateCmd.Parameters.AddWithValue("val2", SqlDbType.Int).Value = 26;
            //updateCmd.Parameters.AddWithValue("val3", SqlDbType.Int).Value = 1;
            //updateCmd.Parameters.AddWithValue("val4", SqlDbType.Int).Value = 9;
 
            //int sodong = updateCmd.ExecuteNonQuery();
            //Console.WriteLine(sodong);
 
            /*---------------------------------------------------------------------*/
 
            /*--------------------------------LỆNH DELETE-------------------------------------*/
 
            SqlCommand delCmd = new SqlCommand();
            delCmd.Connection = con;
            delCmd.CommandText = @"delete from NhanVien where nhanvienid=@id";
            delCmd.Parameters.AddWithValue("id"SqlDbType.Int).Value = 9;
 
            int sodong = delCmd.ExecuteNonQuery();
            Console.WriteLine(sodong);
 
            con.Close();
            Console.ReadKey();
        }
    }
}

Không có nhận xét nào:

Đăng nhận xét