Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- string msServer = @"K21504N11\SQLEXPRESS",
- msDatabase = "Sale";
- public SqlConnection dbcon;
- public string constr()
- {
- return "Data Source=" + msServer + "; Initial Catalog=" + msDatabase + "; Integrated Security=True;";
- }
- public bool DbConnect()
- {
- dbcon = new SqlConnection(constr());
- try
- {
- dbcon.Open();
- }
- catch (SqlException e)
- {
- MessageBox.Show("Произошла ошибка при подключении к базе данных: " + e.Message, "", MessageBoxButtons.OK, MessageBoxIcon.Error);
- dbcon = null;
- return false;
- }
- return true;
- }
- /*
- * Отключение от базы данных
- */
- public void DbDisconnect()
- {
- if (dbcon == null)
- {
- return;
- }
- dbcon.Close();
- dbcon = null;
- }
- public void OnDatabaseError()
- {
- MessageBox.Show("Не удалось подключиться к базе данных. Приложение будет закрыто.", "Фатальная ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
- this.Close();
- }
- /*
- * Возвращает количество строк в SQL-запросе SELECT
- */
- public int DbRowsCount(string tableName)
- {
- return DbRowsCount(tableName, "");
- }
- public int DbRowsCount(string tableName, string exp)
- {
- SqlDataReader count1 = new SqlCommand("SELECT COUNT(*) AS count FROM [" + tableName + "]" + (exp != "" ? " WHERE " + exp : ""), dbcon).ExecuteReader();
- count1.Read();
- int count = (int)count1["count"];
- count1.Close();
- return count;
- }
- public int DbFindLastId(string tableName, string column)
- {
- return DbFindLastId(tableName, column, "");
- }
- public int DbFindLastId(string tableName, string column, string exp)
- {
- SqlDataReader count1 = new SqlCommand("SELECT MAX(" + column + ") AS lastid FROM [" + tableName + "]" + (exp != "" ? " WHERE " + exp : ""), dbcon).ExecuteReader();
- count1.Read();
- int count = (int)count1["lastid"];
- count1.Close();
- return count;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement