You are on page 1of 17

@"Da

using System;

using System.Collections.Generic;

using System.Configuration;

using System.Data;

using System.Data.SqlClient;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

namespace TranNgocSon

internal class Program

static void Main(string[] args)

Console.OutputEncoding = Encoding.UTF8;

//Them();

//Sua();

//Xoa();

Hien();

static void Them()

string connectionString =
ConfigurationManager.ConnectionStrings["db_Shop4Training"].ConnectionString;

string proecName = "spCongDan_Get";

using (SqlConnection cnn = new SqlConnection(connectionString))

using (SqlCommand cmd = new SqlCommand(proecName, cnn))

cmd.CommandType = CommandType.StoredProcedure;
using (SqlDataAdapter da = new SqlDataAdapter(cmd))

DataTable CongDanTable = new DataTable("tblCongdan");

da.Fill(CongDanTable);

if (CongDanTable.Rows.Count == 0)

Console.WriteLine("Chua co du lieu");

else

Console.WriteLine("Đã thêm công dân thành công");

#region câu 1

DataRow newCongDan = CongDanTable.NewRow();

newCongDan["PK_iCongdanID"] = 5;

newCongDan["sHoten"] = "Lan Anh";

newCongDan["tNgaysinh"] = "2002-05-19";

newCongDan["sCMND"] = 127;

newCongDan["sHokhau"] = "LA";

CongDanTable.Rows.Add(newCongDan);

//

SqlCommand insertCmd = new SqlCommand("tblCongdan_Insert",


cnn);

insertCmd.CommandType = CommandType.StoredProcedure;

insertCmd.Parameters.Add("@PK_iCongdanID",
SqlDbType.Int).SourceColumn = "PK_iCongdanID";

insertCmd.Parameters.Add("@sHoten", SqlDbType.NVarChar,
10).SourceColumn = "sHoten";

insertCmd.Parameters.Add("@tNgaysinh",
SqlDbType.Date).SourceColumn = "tNgaysinh";

insertCmd.Parameters.Add("@sCMND",
SqlDbType.Int).SourceColumn = "sCMND";

insertCmd.Parameters.Add("@sHokhau", SqlDbType.NVarChar,
10).SourceColumn = "sHokhau";

//

da.InsertCommand = insertCmd;
DataTable dt = CongDanTable.GetChanges();

da.Update(dt);

if (!dt.HasErrors)

dt.AcceptChanges();

//

#endregion

#region Câu 2

Console.WriteLine("Danh sách công dân");

Console.WriteLine("Họ tên - Ngày sinh - CMND - Hộ khẩu");

DataView dv = new DataView(CongDanTable);

dv.Sort = "PK_iCongdanID";

foreach(DataRowView Display in dv)

Console.WriteLine(string.Format("{0,-4 } {1,-5} {2,-5 }


{3,-9 } ",Display["sHoten"],Display["tNgaysinh"],Display["sCMND"],Display["sHokhau"]));

#endregion

Console.ReadLine();

static void Sua()

string connectionString =
ConfigurationManager.ConnectionStrings["db_Shop4Training"].ConnectionString;

string proecName = "spCongDan_Get";

using (SqlConnection cnn = new SqlConnection(connectionString))

using (SqlCommand cmd = new SqlCommand(proecName, cnn))


{

cmd.CommandType = CommandType.StoredProcedure;

using (SqlDataAdapter da = new SqlDataAdapter(cmd))

DataTable CongDanTable = new DataTable("tblCongdan");

da.Fill(CongDanTable);

if (CongDanTable.Rows.Count == 0)

Console.WriteLine("Chua co du lieu");

else

#region câu 3

Console.WriteLine("Nhập mã công dân");

int index = int.Parse(Console.ReadLine());

Console.WriteLine("Nhập ID cần sửa");

int id = int.Parse(Console.ReadLine());

Console.WriteLine("Nhập Họ tên cần sửa");

string hoten = Console.ReadLine();

Console.WriteLine("Nhập ngày cần sửa");

string ngaysinh = Console.ReadLine();

Console.WriteLine("Nhập cmnd cần sửa");

int cmnd = int.Parse(Console.ReadLine());

Console.WriteLine("Nhập hộ khẩu cần sửa");

string hokhau = Console.ReadLine();

string UpdateString = "Update tblCongdan SET PK_iCongdanID =


@PK_iCongdanID,sHoten = @sHoten,tNgaysinh = @tNgaysinh,sCMND = @sCMND,sHokhau=@sHokhau
WHERE PK_iCongdanID = @idCongdan ";

SqlCommand command = cnn.CreateCommand();

command.CommandText = UpdateString;

command.Parameters.AddWithValue("@idCongdan", index);

command.Parameters.AddWithValue("@PK_iCongdanID", id);
command.Parameters.AddWithValue("@sHoten", hoten);

command.Parameters.AddWithValue("@tNgaysinh", ngaysinh);

command.Parameters.AddWithValue("@sCMND", cmnd);

command.Parameters.AddWithValue("@sHokhau", hokhau);

cnn.Open();

command.ExecuteNonQuery();

cnn.Close();

Console.WriteLine("Đã sửa công dân thành công");

#endregion

Console.ReadLine();

static void Xoa()

string connectionString =
ConfigurationManager.ConnectionStrings["db_Shop4Training"].ConnectionString;

string proecName = "spCongDan_Get";

using (SqlConnection cnn = new SqlConnection(connectionString))

using (SqlCommand cmd = new SqlCommand(proecName, cnn))

cmd.CommandType = CommandType.StoredProcedure;

using (SqlDataAdapter da = new SqlDataAdapter(cmd))

DataTable CongDanTable = new DataTable("tblCongdan");

da.Fill(CongDanTable);

if (CongDanTable.Rows.Count == 0)

{
Console.WriteLine("Chua co du lieu");

else

#region câu 3

Console.WriteLine("Nhập mã công dân cần xóa");

int index = int.Parse(Console.ReadLine());

string UpdateString = "DELETE dbo.tblCongdan WHERE sCMND =


@idCongdan ";

cnn.Open();

using (SqlCommand command = new SqlCommand(UpdateString,


cnn))

command.Parameters.AddWithValue("@idCongdan", index);

command.ExecuteNonQuery();

cnn.Close();

Console.WriteLine("Đã xóa công dân thành công");

#endregion

Console.ReadLine();

static void Hien()

{
string connectionString =
ConfigurationManager.ConnectionStrings["db_Shop4Training"].ConnectionString;

string proecName = "spCongDan_Get";

using (SqlConnection cnn = new SqlConnection(connectionString))

using (SqlCommand cmd = new SqlCommand(proecName, cnn))

cmd.CommandType = CommandType.StoredProcedure;

using (SqlDataAdapter da = new SqlDataAdapter(cmd))

DataTable CongDanTable = new DataTable("tblCongdan");

da.Fill(CongDanTable);

if (CongDanTable.Rows.Count == 0)

Console.WriteLine("Chua co du lieu");

else

#region câu 3

Console.WriteLine("Nhập mã công dân cần hiển thị");

int index = int.Parse(Console.ReadLine());

Console.WriteLine("Danh sách công dân:");

string UpdateString = "tblCongdan_Display";

cnn.Open();

SqlCommand command = new SqlCommand(UpdateString, cnn);

command.CommandType = CommandType.StoredProcedure;

command.Parameters.Add(new SqlParameter()

ParameterName = "@id",

SqlDbType = SqlDbType.Int,

Value = index

});
using (SqlDataReader reader = command.ExecuteReader())

while (reader.Read())

var ID = reader["PK_iCongdanID"];

var Hoten = reader["sHoten"];

var Ngaysinh = reader["tNgaysinh"];

var Cmnd = reader["sCMND"];

var Hokhau = reader["sHokhau"];

Console.WriteLine("Id \t Họ tên \t Ngày sinh \t \t


CMND \t Hộ khẩu");

Console.WriteLine($"{ID} \t {Hoten} \t
{Ngaysinh} \t {Cmnd} \t {Hokhau}");

cnn.Close();

#endregion

Console.ReadLine();

1213

using System;

using System.Collections.Generic;

using System.Text;
using System.Data.SqlClient;

using System.Data;

namespace BTVN_Buoi3

class Program

public static void inSert()

string connectionString = @"Data Source=.\SQLExpress;Initial Catalog=BT_Buoi2;Integrated


Security=True";

string select = "SELECT *from tblCongdan";

using (SqlConnection cnn = new SqlConnection(connectionString))

cnn.Open();

using (SqlCommand cmd = new SqlCommand(select, cnn))

cmd.CommandType = System.Data.CommandType.StoredProcedure;

cmd.CommandText = "themmoi";

Console.WriteLine("Nhap so nguoi can them:");

int n = Convert.ToInt32(Console.ReadLine());

for (int i = 0; i < n; i++)

Console.WriteLine("Nhap id");

string id = Console.ReadLine();

cmd.Parameters.AddWithValue("ID", id);
Console.WriteLine("Nhap Ho ten");

String hoten = Console.ReadLine();

cmd.Parameters.AddWithValue("Hoten", hoten);

Console.WriteLine("Nhap ngay sinh");

int ngaysinh = Convert.ToInt32(Console.ReadLine());

Console.WriteLine("Nhap Thang:");

int thang = Convert.ToInt32(Console.ReadLine());

Console.WriteLine("nHập năm:");

int nam = Convert.ToInt32(Console.ReadLine());

DateTime namsinh = new DateTime(nam, thang, ngaysinh);

cmd.Parameters.AddWithValue("Ngaysinh", namsinh);

Console.WriteLine("Nhapn CMND");

string CMND = Console.ReadLine(); ;

cmd.Parameters.AddWithValue("CMND", CMND);

Console.WriteLine("Nhap Ho khau");

string Hokhau = Console.ReadLine(); ;

cmd.Parameters.AddWithValue("Hokhau", Hokhau);

var kq = cmd.ExecuteNonQuery();

if (kq == 1)

Console.WriteLine("Cap nhap thanh cong");

else

Console.WriteLine("Cap nhap that bai");


}

cnn.Close();

public static void hien()

string connectionString = @"Data Source=.\SQLExpress;Initial Catalog=BT_Buoi2;Integrated


Security=True";

string select = "SELECT* from tblCongdan";

using (SqlConnection cnn = new SqlConnection(connectionString))

cnn.Open();

using (SqlCommand cmd = new SqlCommand(select, cnn))

cmd.CommandType = CommandType.StoredProcedure;

cmd.CommandText = "hien";

using(SqlDataAdapter da = new SqlDataAdapter(cmd))

DataTable congdan = new DataTable("tblCongdan");

da.Fill(congdan);

if (congdan.Rows.Count == 0)

{
Console.WriteLine("Chua co du lieu");

else

Console.WriteLine("Danh sanh cong dan");

foreach(DataRow dr in congdan.Rows)

Console.WriteLine("{0} - {1} - {2} - {3} - {4}"

,dr["iCongdanID"]

,dr["sHoten"]

,dr["tNgaysinh"]

,dr["sCMND"]

,dr["sHokhau"]);

Console.ReadLine();

}cnn.Close();

public static void update()

string connectionString = @"Data Source=.\SQLExpress;Initial Catalog=BT_Buoi2;Integrated


Security=True";

string select = "SELECT *from tblCongdan";

using (SqlConnection cnn = new SqlConnection(connectionString))


{

cnn.Open();

using (SqlCommand cmd =new SqlCommand(select,cnn))

cmd.CommandType = CommandType.StoredProcedure;

cmd.CommandText = "updatetblCongdan";

Console.WriteLine("Nhap id can update");

int ID = Convert.ToInt32(Console.ReadLine());

cmd.Parameters.AddWithValue("ID", ID);

Console.WriteLine("Nhap Ho ten");

String hoten = Console.ReadLine();

cmd.Parameters.AddWithValue("Hoten", hoten);

Console.WriteLine("Nhap ngay sinh");

int ngaysinh = Convert.ToInt32(Console.ReadLine());

Console.WriteLine("Nhap Thang:");

int thang = Convert.ToInt32(Console.ReadLine());

Console.WriteLine("nHập năm:");

int nam = Convert.ToInt32(Console.ReadLine());

DateTime namsinh = new DateTime(nam, thang, ngaysinh);

cmd.Parameters.AddWithValue("Ngaysinh", namsinh);

Console.WriteLine("Nhapn CMND");

string CMND = Console.ReadLine(); ;

cmd.Parameters.AddWithValue("CMND", CMND);
Console.WriteLine("Nhap Ho khau");

string Hokhau = Console.ReadLine(); ;

cmd.Parameters.AddWithValue("Hokhau", Hokhau);

var kq = cmd.ExecuteNonQuery();

if (kq == 1)

Console.WriteLine("Cap nhap thanh cong");

else

Console.WriteLine("Cap nhap that bai");

cnn.Close();

public static void Xoa()

string connectionString = @"Data Source=.\SQLExpress;Initial Catalog=BT_Buoi2;Integrated


Security=True";

string select = "SELECT *from tblCongdan";

using (SqlConnection cnn = new SqlConnection(connectionString))

cnn.Open();

using (SqlCommand cmd = new SqlCommand(select, cnn))

int xoa;

cmd.CommandType = CommandType.Text;
cmd.CommandText = @"DELETE dbo.tblCongdan where iCongdanID=@xoa";

Console.WriteLine("Nhap ID can xoa:");

xoa = Convert.ToInt32(Console.ReadLine());

cmd.Parameters.AddWithValue("xoa", xoa);

var kq = cmd.ExecuteNonQuery();

if (kq == 1)

Console.WriteLine("Cap nhap thanh cong");

else

Console.WriteLine("Cap nhap that bai hoac khong co id trong mang");

cnn.Close();

public static void timtheoTen()

string connectionString = @"Data Source=.\SQLExpress;Initial Catalog=BT_Buoi2;Integrated


Security=True";

string select = "SELECT *from tblCongdan";

using (SqlConnection cnn = new SqlConnection(connectionString))

cnn.Open();

using (SqlCommand cmd = new SqlCommand(select, cnn))

cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandText = "timkiemtheoTen";

Console.WriteLine("nhap ho ten can tim kiem:");

string hoten = Console.ReadLine();

cmd.Parameters.AddWithValue("@Hoten", hoten);

using(SqlDataAdapter da= new SqlDataAdapter(cmd))

DataTable tim = new DataTable();

da.Fill(tim);

if(tim.Rows.Count==0)

Console.WriteLine("Khong co ten nguoi dung trong bang");

else

Console.WriteLine("Danh sach ten nguoi can tim: ");

foreach(DataRow dr in tim.Rows)

Console.WriteLine("{0} - {1} - {2} - {3} - {4}"

, dr["iCongdanID"]

, dr["sHoten"]

, dr["tNgaysinh"]

, dr["sCMND"]

, dr["sHokhau"]);

}
}cnn.Close();

static void Main(string[] args)

//inSert();

hien();

// update();

//Xoa();

timtheoTen();

Console.ReadKey();

You might also like