using System.Data;
using System.Data.OleDb;
using System.Windows.Forms;
namespace InfoManger.MobDbHelper
{
class DbHelper
{
public static OleDbConnection conn = null;
private static void InitConnection()
{
try
{
if (conn == null)
conn = new OleDbConnection(ConnectionString.StrCnn);
if (conn.State == ConnectionState.Broken)
{
conn.Close();
conn.Open();
}
if (conn.State == ConnectionState.Closed)
{
conn.Open();
}
}
catch (OleDbException Ex)
{
MessageBox.Show(Ex.Message);
conn.Close();
}
}
public static OleDbDataReader GetOleDbDataReader(string str)
{
InitConnection();
try
{
OleDbCommand cmd = new OleDbCommand(str, conn);
return cmd.ExecuteReader(CommandBehavior.CloseConnection);
}
catch (OleDbException Ex)
{
MessageBox.Show(Ex.Message);
return null;
}
}
public static DataSet GetDataSet(string sqlStr)
{
InitConnection();
try
{
DataSet ds = new DataSet();
OleDbDataAdapter dap = new OleDbDataAdapter(sqlStr, conn);
dap.Fill(ds);
return ds;
}
catch (OleDbException Ex)
{
MessageBox.Show(Ex.Message);
return null;
}
}
public static DataTable GetDataTable(string sqlStr)
{
try
{
return GetDataSet(sqlStr).Tables[0];
}
catch (OleDbException Ex)
{
MessageBox.Show(Ex.Message);
return null;
}
}
public static int ExecuteNonQuery(string sqlStr)
{
InitConnection();
try
{
OleDbCommand cmd = new OleDbCommand(sqlStr, conn);
int result = cmd.ExecuteNonQuery();
return result;
}
catch (OleDbException Ex)
{
MessageBox.Show(Ex.Message);
return 0;
}
}
public static object ExecuteScalar(string sqlStr)
{
InitConnection();
try
{
OleDbCommand cmd = new OleDbCommand(sqlStr, conn);
object result = cmd.ExecuteScalar();
return result;
}
catch (OleDbException Ex)
{
MessageBox.Show(Ex.Message);
return null;
}
}
}
}