using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.OleDb;
namespace Carservice
{
public class dbm
{
public static string SqlCom { get; set; }
private static string conString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=|DataDirectory|\\DatabaseCarservice.accdb";
public static void ExeSql(string sql) {
using(OleDbConnection con = new OleDbConnection(conString)){
con.Open();
using (OleDbCommand cmd = new OleDbCommand(sql, con)) {
try
{
cmd.ExecuteNonQuery();
}
catch (Exception e)
{
DevExpress.XtraEditors.XtraMessageBox.Show(e.ToString());
}
}
}
}
public static void ExeSql() {
using (OleDbConnection con = new OleDbConnection(conString))
{
con.Open();
using (OleDbCommand cmd = new OleDbCommand(SqlCom, con))
{
try
{
cmd.ExecuteNonQuery();
}
catch (Exception e)
{
DevExpress.XtraEditors.XtraMessageBox.Show(e.ToString());
}
}
}
}
public static DataTable GetDataTable(string sql) {
using (OleDbConnection con = new OleDbConnection(conString))
{
con.Open();
DataTable dt = new DataTable();
using (OleDbDataAdapter dap = new OleDbDataAdapter(sql, con))
{
dap.Fill(dt);
return dt;
}
}
}
public static DataTable GetDataTable()
{
using (OleDbConnection con = new OleDbConnection(conString))
{
con.Open();
DataTable dt = new DataTable();
using (OleDbDataAdapter dap = new OleDbDataAdapter(SqlCom, con))
{
dap.Fill(dt);
return dt;
}
}
}
public static DataSet GetDataSet(string sql) {
using (OleDbConnection con = new OleDbConnection(conString))
{
con.Open();
DataSet ds = new DataSet();
using (OleDbDataAdapter dap = new OleDbDataAdapter(sql, con))
{
dap.Fill(ds);
return ds;
}
}
}
public static DataSet GetDataSet()
{
using (OleDbConnection con = new OleDbConnection(conString))
{
con.Open();
DataSet ds = new DataSet();
using (OleDbDataAdapter dap = new OleDbDataAdapter(SqlCom, con))
{
dap.Fill(ds);
return ds;
}
}
}
public static bool CheckExist(string sql) {
using (OleDbConnection con = new OleDbConnection(conString))
{
con.Open();
using (OleDbCommand cmd = new OleDbCommand(sql, con))
{
OleDbDataReader dr = cmd.ExecuteReader();
dr.Read();
if (dr.HasRows)
{
return true;
}
else
{
return false;
}
}
}
}
public static bool CheckExist()
{
using (OleDbConnection con = new OleDbConnection(conString))
{
con.Open();
using (OleDbCommand cmd = new OleDbCommand(SqlCom, con))
{
OleDbDataReader dr = cmd.ExecuteReader();
dr.Read();
if (dr.HasRows)
{
return true;
}
else
{
return false;
}
}
}
}
public static string Get1Value(string sql) {
using (OleDbConnection con = new OleDbConnection(conString))
{
con.Open();
using (OleDbCommand cmd = new OleDbCommand(sql, con))
{
OleDbDataReader dr = cmd.ExecuteReader();
dr.Read();
if (dr.HasRows)
{
return dr[0].ToString();
}
else
{
return null;
}
}
}
}
public static string Get1Value()
{
using (OleDbConnection con = new OleDbConnection(conString))
{
con.Open();
using (OleDbCommand cmd = new OleDbCommand(SqlCom, con))
{
OleDbDataReader dr = cmd.ExecuteReader();
dr.Read();
if (dr.HasRows)
{
return dr[0].ToString();
}
else
{
return null;
}
}
}
}
}
}
เขียน class ไว้ แล้วให้มันเรียกใช้ จะได้ไม่ต้องติดต่อฐานข้อมูลบ่อยๆ โค้ดน้อยด้วย