自己寫的一套處理數據庫的方法
2024-07-21 02:45:02
供稿:網友
class COperateAcc
{
PRivate SqlConnection conn = null;//連接數據庫
private SqlCommand comm = null;//操作數據庫sql
private SqlDataAdapter ada = null;//填充dataset
private SqlDataReader reader = null;//讀取數據庫表中的值
//構造函數
//初始化連接數據庫
public COperateAcc()
{
try
{
DBCon dbcon = new DBCon();
conn = dbcon.getConn();
}
catch (Exception e)
{
throw e;
}
finally
{
close();
}
}
//讀完數據庫值關閉所有連接
//通過判斷看所有連接是否關閉,沒關閉的情況關閉
public void close()
{
if (reader != null)
{
reader.Close();
}
if (conn.State == ConnectionState.Open)
{
conn.Close();
}
}
//功能:查詢sql語句
public SqlDataReader executeReader(string str_SqlString)
{
try
{
if (conn.State == ConnectionState.Open)
{
conn.Close();
}
comm = new SqlCommand();
comm.CommandText = str_SqlString;
comm.Connection = conn;
conn.Open();
reader = comm.ExecuteReader(CommandBehavior.CloseConnection);
}
catch (Exception e)
{
throw e;
}
return reader;
}
//功能:獲得分頁的行數
public int getPageRecord(string str_SqlString)
{
int count;
try
{
comm = new SqlCommand();
comm.CommandText = str_SqlString;
comm.Connection = conn;
conn.Open();
count = (int)comm.ExecuteScalar();
}
catch (Exception e)
{
throw e;
}
finally
{
close();
}
return count;
}
//功能:把從數據庫表中得到的數據保存到客戶端的一張表中
//返回:dataset 已一張表的形式保存到dataset
//參數:str_SqlString 查詢的sql語句
public DataSet getDataSet(string str_SqlString)
{
DataSet ds = new DataSet();
try
{
ada = new SqlDataAdapter(str_SqlString, conn);
ada.Fill(ds);
}
catch (Exception e)
{
throw e;
}
finally
{
close();
}
return ds;
}
//功能:把從數據庫表中得到的數據保存到客戶端的一張表中
//返回:datatable 已一張表的形式保存到datatable
//參數:str_SqlString 查詢的sql語句
public DataTable getDataTable(string str_SqlString)
{
DataTable dt = new DataTable();
try
{
ada = new SqlDataAdapter(str_SqlString, conn);
ada.Fill(dt);
}
catch (Exception e)
{
throw e;
}
finally
{
close();
}
return dt;
}
//功能:操作數據庫中的表 添加,刪除,更新
//返回:int 受影響的行數總數。
//參數:str_SqlString sql語句
public int executeSql(string str_SqlString)
{
int int_Num = 0;
try
{
comm = new SqlCommand();
comm.CommandText = str_SqlString;
comm.Connection = conn;
conn.Open();
int_Num = comm.ExecuteNonQuery();
}
catch (Exception e)
{
throw e;
}
finally
{
close();
}
return int_Num;
}
//功能:生成數據庫中最大的唯一編號(掛失表等,總共19位 str_Szf+日期+四位數字)
//返回:string 最大值
//參數:@par1:str_Szf 用戶自定義字符串 (前兩位為用戶自己規定+后五位位用戶登錄時的單位編號)
// :@par2:str_Id 數據庫唯一編號字段名
// :@par3:str_TableName 數據庫表名
public string getMaxID()
{
string str_MaxNum = "";
try
{
string sql = "select max(OId) from OrderRegister where OId like '%' ";
reader = this.executeReader(sql);
reader.Read();
if (reader[0].ToString().Equals(""))
{
str_MaxNum = this.getDateZh(this.getDate()) + "01";
}
else
{
if (reader[0].ToString().Substring(0, 8).Equals(this.getDateZh(this.getDate())))
{
str_MaxNum = this.getDateZh(this.getDate()) + Convert.ToString((Convert.ToInt32(reader[0].ToString().Substring(8, 2)) + 1)).PadLeft(2, '0');
}
else
{
str_MaxNum = this.getDateZh(this.getDate()) + "01";
}
}
}
catch (Exception e)
{
throw e;
}
finally
{
close();
}
return str_MaxNum;
}
//得到當天日期(年+月+日)
public string getDate()
{
string date = Convert.ToString(DateTime.Now).Substring(0, Convert.ToString(DateTime.Now).IndexOf(' '));
return date;
}
//轉換日期形式
public string getDateZh(string str_DateValue)
{
string str_DValue;
if (str_DateValue.Length == 8)//八位 例如2008-1-1
{
str_DValue = str_DateValue.Substring(0, 4) + "0" + str_DateValue.Substring(5, 1) + "0" + str_DateValue.Substring(7, 1);
}
else if (Convert.ToInt32(str_DateValue.Length) == 9)//九位 分別為:2008-11-6,2008-2-12
{
if (str_DateValue.Substring(6, 1) == "-")
{
str_DValue = str_DateValue.Substring(0, 4) + "0" + str_DateValue.Substring(5, 1) + str_DateValue.Substring(7, 2);
}
else
{
str_DValue = str_DateValue.Substring(0, 4) + str_DateValue.Substring(5, 2) + "0" + str_DateValue.Substring(8, 1);
}
}
else//十位 例如:2008-11-23
{
str_DValue = str_DateValue.Substring(0, 4) + str_DateValue.Substring(5, 2) + str_DateValue.Substring(8, 2);
}
return str_DValue;
}
}
如果是access數據庫 將SqlConnection SqlCommand 等換成 OleDbConnection OleDbCommand 就可以了。
本文來自CSDN博客,轉載請標明出處:http://blog.csdn.net/yangliuyilovexi/archive/2009/12/17/5027382.aspx