using System;
using System.Data;
using System.Data.OleDb;
using System.Configuration;
using System.Web;
namespace MCF
{
/// <summary>
/// Cdatabase 的摘要说明。
/// </summary>
public class Cdatabase
{
private OleDbConnection AccessConn=new System.Data.OleDb.OleDbConnection();
public Cdatabase()
{
//
// TODO: 在此处添加构造函数逻辑
//
}
#region"用于关闭连接"
private void ConnClose()
{
if(ConnState())
{
AccessConn.Close();
}
}
#endregion
#region"ConnState 用于返回当前连接的状态"
private bool ConnState()
{
if( AccessConn.State.ToString()=="Open")
{
return true;
}
else
{
return false;
}
}
#endregion
#region"Open 用于打开数据库的方法"
/// <summary>
/// 用于打开Access数据库
/// </summary>
/// <returns>返回类型为 Bool 类型</returns>
private bool Open()
{
try
{
string dbm=HttpContext.Current.Server.MapPath( ConfigurationSettings.AppSettings["DataBaseName"]);
AccessConn.ConnectionString=@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + dbm; //+ ";User ID=Admin;Database Password=";
AccessConn.Open();
return true;
}
catch
{
return false;
}
//Msgbox._Show("数据库已经打开!",FireTiger_ControlLibrary.FireTiger_Dialog.MsgBoxStyle.Information,false,this.Msgbox_Title );
//System.Windows.Forms.MessageBox.Show("数据库已经打开!");
}
#endregion
#region"RunSQLcmd 用于运行SQL语句的方法"
/// <summary>
/// 用于运行SQL语句的方法
/// 返回值为一个bool
/// </summary>
/// <param name="CmdStr">要运行的 SQL 语句</param>
/// <returns>返回值为 bool 类型 </returns>
public bool RunSQLcmd(string CmdStr)
{
try
{
if(Open())
{
OleDbCommand cmd1=new OleDbCommand("",AccessConn);
cmd1.CommandText=@CmdStr;
cmd1.ExecuteNonQuery() ;//运行SQL语句
return true;
}
else
{
return false;
}
}
catch
{
return false;
}
finally
{
ConnClose();
}
}
#endregion
#region "RunSQLcmdDataSet 用于运行SQL语句的方法"
/// <summary>
/// 用于运行SQL语句的方法
/// 返回值为一个DataSet
/// 注意此方法与 RunSQLcmd 的不同
/// </summary>
/// <param name="CmdStr">要运行的 SQL 语句</param>
/// <returns>返回值为 DataSet 类型 </returns>
public DataSet RunSQLcmdDataSet( string CmdStr)
{
try
{
if(Open())
{
DataSet MyDataSet=new DataSet();
OleDbDataAdapter myaccessdp=new OleDbDataAdapter();
myaccessdp.SelectCommand=new OleDbCommand( @CmdStr,AccessConn);
myaccessdp.Fill(MyDataSet);
return MyDataSet;
}
return null;
}
catch(Exception ex)
{
// Msgbox._Show(Ex.Message,FireTiger_ControlLibrary.FireTiger_Dialog.MsgBoxStyle.Information,false,"在runsqlcmd的方法中出错");
//System.Windows.Forms.MessageBox.Show(Ex.Message,"在runsqlcmd的方法中出错");
return null;
}
finally
{
ConnClose();
}
}
#endregion
#region "用于把一个表的数据绑定到一个下拉列表框中去"
/// <summary>
/// 用于把一个表的数据绑定到一个下拉列表框中去
/// </summary>
/// <param name="YouDropDownList">要绑定的下拉列表框</param>
/// <param name="Sqlcmd">SQL命令</param>
/// <param name="ShowText">下列要显示的值</param>
/// <param name="ShowValue">下列实际的值</param>
public void bingDataToDropDownList(System.Web.UI.WebControls.DropDownList YouDropDownList, string Sqlcmd,string ShowText,string ShowValue)
{
DataTable temptable=new DataTable();
temptable=RunSQLcmdDataSet(Sqlcmd).Tables[0];
YouDropDownList.DataSource=temptable;
YouDropDownList.DataTextField=ShowText;
YouDropDownList.DataValueField=ShowValue;
YouDropDownList.DataBind();
}
#endregion
}
}