Advertisement
Guest User

Untitled

a guest
Oct 16th, 2019
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.54 KB | None | 0 0
  1. string msServer = @"K21504N11\SQLEXPRESS",
  2.                msDatabase = "Sale";
  3.         public SqlConnection dbcon;
  4.  
  5.         public string constr()
  6.         {
  7.             return "Data Source=" + msServer + "; Initial Catalog=" + msDatabase + "; Integrated Security=True;";
  8.         }
  9.  
  10.         public bool DbConnect()
  11.         {
  12.             dbcon = new SqlConnection(constr());
  13.  
  14.             try
  15.             {
  16.                 dbcon.Open();
  17.             }
  18.             catch (SqlException e)
  19.             {
  20.                 MessageBox.Show("Произошла ошибка при подключении к базе данных: " + e.Message, "", MessageBoxButtons.OK, MessageBoxIcon.Error);
  21.                 dbcon = null;
  22.                 return false;
  23.             }
  24.             return true;
  25.         }
  26.  
  27.         /*
  28.          * Отключение от базы данных
  29.          */
  30.         public void DbDisconnect()
  31.         {
  32.             if (dbcon == null)
  33.             {
  34.                 return;
  35.             }
  36.             dbcon.Close();
  37.             dbcon = null;
  38.         }
  39.  
  40.         public void OnDatabaseError()
  41.         {
  42.             MessageBox.Show("Не удалось подключиться к базе данных. Приложение будет закрыто.", "Фатальная ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
  43.             this.Close();
  44.         }
  45.  
  46.         /*
  47.          * Возвращает количество строк в SQL-запросе SELECT
  48.          */
  49.  
  50.         public int DbRowsCount(string tableName)
  51.         {
  52.             return DbRowsCount(tableName, "");
  53.         }
  54.  
  55.         public int DbRowsCount(string tableName, string exp)
  56.         {
  57.             SqlDataReader count1 = new SqlCommand("SELECT COUNT(*) AS count FROM [" + tableName + "]" + (exp != "" ? " WHERE " + exp : ""), dbcon).ExecuteReader();
  58.             count1.Read();
  59.             int count = (int)count1["count"];
  60.             count1.Close();
  61.             return count;
  62.         }
  63.  
  64.         public int DbFindLastId(string tableName, string column)
  65.         {
  66.             return DbFindLastId(tableName, column, "");
  67.         }
  68.  
  69.         public int DbFindLastId(string tableName, string column, string exp)
  70.         {
  71.             SqlDataReader count1 = new SqlCommand("SELECT MAX(" + column + ") AS lastid FROM [" + tableName + "]" + (exp != "" ? " WHERE " + exp : ""), dbcon).ExecuteReader();
  72.             count1.Read();
  73.             int count = (int)count1["lastid"];
  74.             count1.Close();
  75.             return count;
  76.         }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement