公共类直接调方法
C# codeusing System;
using System.Collections.Generic;
using System.Data;
using System.Data.OracleClient;
using System.Web.UI.WebControls;
namespace DBClass
{
public class DBClass
{
#region -- 获取数据库的连接 GetOracleConnection()
/// <summary>
/// 获取数据库的连接
/// </summary>
/// <returns></returns>
public OracleConnection GetOracleConnection()
{
string strCon =
System.Configuration.ConfigurationManager.AppSettings["strConnect"].ToString
();
try
{
OracleConnection myConn = new OracleConnection(strCon);
myConn.Open();
return myConn;
}
catch (Exception ex)
{
string error = ex.Message;
return null;
}
}
#endregion
#region -- 获取第一行的第一列 GetFistString(string strSql)
/// <summary>
/// 获取第一行的第一列
/// </summary>
/// <param name="strSql"></param>
/// <returns></returns>
public string GetFistString(string strSql)
{
OracleConnection conn = GetOracleConnection();
OracleCommand myComm = new OracleCommand(strSql, conn);
try
{
return myComm.ExecuteScalar().ToString();
}
catch (Exception ex)
{
string error = ex.Message;
return "0";
}
finally
{
conn.Close();
myComm.Dispose();
}
}
#endregion
#region -- 对数据库中的一条记录操作:增、删、更新 ExecuteCommand
(string strSql)
/// <summary>
/// 对数据库中的一条记录操作:增、删、更新
/// </summary>
/// <param name="strSql"></param>
/// <returns></returns>
public bool ExecuteCommand(string strSql)
{
OracleConnection conn = GetOracleConnection();
OracleCommand myComm = new OracleCommand(strSql, conn);
try
{
int temp = myComm.ExecuteNonQuery();
return temp == 1;
}
catch (Exception ex)
{
string error = ex.Message;
string errIns = "INSERT INTO ERRORS (ERR_TITLE, ERR_DESC)
VALUES ('Ins-del-upd','" + error + "')";
OracleConnection errCnn = GetOracleConnection();
OracleCommand errComm = new OracleCommand(errIns, errCnn);
errComm.ExecuteNonQuery();
errCnn.Close();
errComm.Dispose();
return false;
}
finally
{
conn.Close();
myComm.Dispose();
}
}
#endregion
#region --得到一个DataSet对象 GetDateSet(string strSql)
public DataSet GetDateSet(string strSql)
{
OracleConnection myConn = GetOracleConnection();
OracleDataAdapter adap = new OracleDataAdapter(strSql, myConn);
try
{
DataSet myDS = new DataSet();
adap.Fill(myDS);
return myDS;
}
catch (Exception ex)
{
string error = ex.Message;
return null;
}
finally
{
myConn.Close();
adap.Dispose();
}
}
#endregion
#region --传入SQL语句获取一个DataReader对象 GetDataReader(string
strSql)
public OracleDataReader GetDataReader(string strSql)
{
OracleConnection myConn = GetOracleConnection();
try
{
OracleCommand myComm = new OracleCommand(strSql, myConn);
return myComm.ExecuteReader(CommandBehavior.CloseConnection);
}
catch (Exception ex)
{
string error = ex.Message;
return null;
}
}
#endregion
#region --填充DataGrid公共方法 FillDataGrid
(System.Web.UI.WebControls.DataGrid dGrid, string strSql)
public void FillDataGrid(System.Web.UI.WebControls.DataGrid dGrid,
string strSql)
{
try
{
OracleConnection strConnection = GetOracleConnection();
OracleDataAdapter myDA = new OracleDataAdapter(strSql,
strConnection);
DataSet myDS = new DataSet();
myDA.Fill(myDS);
//int i=myDS.Tables[0].Rows.Count;
dGrid.DataSource = myDS.Tables[0];
dGrid.DataBind();
}
catch (Exception ex)
{
string error = ex.Message;
}
}
#endregion
#region --填充DropDownList FillDropDownList
(System.Web.UI.WebControls.DropDownList cmbList, string strSql)
public void FillDropDownList(System.Web.UI.WebControls.DropDownList
cmbList, string strSql)
{
try
{
using (OracleConnection MyCnn = GetOracleConnection())
{
OracleCommand sqlCmm = new OracleCommand(strSql, MyCnn);
OracleDataReader sqlDR = sqlCmm.ExecuteReader();
cmbList.Items.Clear();
while (sqlDR.Read())
{
ListItem li = new ListItem(sqlDR[1].ToString().Trim
(), sqlDR[0].ToString().Trim());
cmbList.Items.Add(li);
}
MyCnn.Close();
sqlDR.Close();
sqlCmm.Dispose();
}
}
catch(Exception ex)
{
string error = ex.Message;
}
}
#endregion
#region 填充下拉列表,第一个值为自定义 FillDropDownListFirstAdd
(System.Web.UI.WebControls.DropDownList cmbList, string strSql, string
firstItem)
/// <summary>
/// 填充下拉列表,第一个值为自定义
/// </summary>
/// <param name="cmbList">要填充的DropdownList</param>
/// <param name="strSql">要使用的sql语句:要求:第一列为Value,第二列
为Text</param>
/// <param name="firstItem">列表中第一个 Item 的值</param>
/// <returns>是否成功</returns>
public bool FillDropDownListFirstAdd
(System.Web.UI.WebControls.DropDownList cmbList, string strSql, string
firstItem)
{
try
{
using (OracleConnection MyCnn = GetOracleConnection())
{
OracleCommand sqlCmm = new OracleCommand(strSql, MyCnn);
OracleDataReader sqlDR = sqlCmm.ExecuteReader();
cmbList.Items.Clear();
//第一条信息
if (firstItem != string.Empty)
{
ListItem li1 = new ListItem(firstItem, "0");
cmbList.Items.Add(li1);
}
while (sqlDR.Read())
{
ListItem li = new ListItem(sqlDR[1].ToString().Trim
(), sqlDR[0].ToString().Trim());
cmbList.Items.Add(li);
}
MyCnn.Close();
sqlDR.Close();
sqlCmm.Dispose();
return true;
}
}
catch
{
return false;
}
}
#endregion
}
}