Advertisement
Guest User

Untitled

a guest
Jun 25th, 2016
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.85 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using System.Data.SqlClient;
  7. using System.Data;
  8. namespace QuanLiBanHang
  9. {
  10.     class XulyDulieu
  11.     {
  12.         SqlConnection con;
  13.         public XulyDulieu()
  14.         {
  15.             this.con = new SqlConnection();
  16.             this.con.ConnectionString= @"Data Source=PHONGVU\SQLEXPRESS;Initial Catalog=QLBH_CS414;Integrated Security=True";
  17.         }
  18.         public void Moketnoi()
  19.         {
  20.             if (con.State == System.Data.ConnectionState.Closed)
  21.                 con.Open();
  22.         }
  23.         public void Dongketnoi()
  24.         {
  25.             if (con.State == System.Data.ConnectionState.Open)
  26.                 con.Close();
  27.         }
  28.         /// <summary>
  29.         /// Thực thi sql(select*from)
  30.         /// Trả về kiểu dl bảng DataTable
  31.         /// </summary>
  32.         /// <param name="sql">sql=select*from...</param>
  33.         /// <returns></returns>
  34.         public DataTable table(string sql)
  35.         {
  36.             this.Moketnoi();
  37.             DataTable tb = new DataTable();
  38.             SqlDataAdapter adp = new SqlDataAdapter(sql, con);
  39.             adp.Fill(tb);
  40.             this.Dongketnoi();
  41.             return tb;
  42.         }
  43.         /// <summary>
  44.         /// Thực thi sql(insert into,delete,update)
  45.         /// Trả về hai giá trị (0 và <>0)
  46.         /// nếu=0 thành công
  47.         /// nếu <> không thành công
  48.         /// </summary>
  49.         /// <param name="sql"></param>
  50.         /// <returns></returns>
  51.         public int ThucthiSql(string sql)
  52.         {
  53.             int result = 0;
  54.             this.Moketnoi();
  55.             SqlCommand cmd = new SqlCommand(sql,con);
  56.             result = (int)cmd.ExecuteNonQuery();
  57.             this.Dongketnoi();
  58.             return result;
  59.         }
  60.     }
  61. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement