求教c#如何写连接数据库的配置文件,谢谢啊

2024-12-27 21:15:41
推荐回答(2个)
回答1:



回答2:

public class Database
{
public Database()
{
//
// TODO: 在此处添加构造函数逻辑
//
}
public OleDbConnection connection;
private void Open()
{
string connString = @"Provider= Microsoft.Jet.OLEDB.4.0;Data Source=" + HttpContext.Current.Server.MapPath("~/App_Data/本科生毕业论文管理系统.mdb");
connection = new OleDbConnection(connString);
connection.Open();
}

private void Close()
{
connection.Dispose();//释放资源
connection.Close();
connection = null;
}
public OleDbDataReader GetDataReader(string sqlString)
{
this.Open();
OleDbCommand comm = new OleDbCommand(sqlString, connection);
OleDbDataReader dr = comm.ExecuteReader();
return dr;
}
public DataSet GetDataSet(string sqlString)
{
this.Open();
OleDbCommand comm = new OleDbCommand(sqlString, connection);
OleDbDataAdapter da = new OleDbDataAdapter();
da.SelectCommand = comm;
DataSet ds = new DataSet();
da.Fill(ds);
this.Close();
return ds;
}
public int ExecuteSQL(string sqlString)
{
int count = -1;

try
{
Open();
OleDbCommand comm = new OleDbCommand(sqlString, connection);
count = comm.ExecuteNonQuery();//执行sql语句并返回
}
catch (Exception e)
{
//MessageBox.Show(e.Message);
count = -1;
}
finally
{
Close();
}
return count;
}
public int GatdataRow(string sqlString)
{
int countrow = 0;
Open();
OleDbCommand comm = new OleDbCommand(sqlString, connection);
OleDbDataAdapter da = new OleDbDataAdapter(comm);
DataSet ds = new DataSet();
da.Fill(ds);
ds.CaseSensitive = false;
countrow = ds.Tables[0].Rows.Count;
Close();
return countrow;
}
public DataTable GetDataTable(string sqlString)
{
DataSet ds = GetDataSet(sqlString);
DataTable dt = new DataTable();
dt = ds.Tables[0];
return dt;
}
public object GetScalar(string sqlString)
{
Open();
OleDbCommand comm = new OleDbCommand(sqlString, connection);
object result = comm.ExecuteScalar();
Close();
return result;
}