以前一直使用的是SQL-Server数据库,很少使用Access数据库,这些天要做一个小东西,就使用起Access数据来,其实两种数据库的操作主要是对数据库的操作方面引用的命名空间不一样。
它们对数据的操作总结如下,有什么不对的地方,请多给宝贵意见。
Access数据库:
首先在命名空间中添加访问Access数据库的引用
using System.Data.OleDb;
********************
public class common
{
protected OleDbConnection conn;
protected OleDbDataReader dr;
public common()
{
//
// TODO: 在此处添加构造函数逻辑
//
}
public string getconnstring()
{
string constr;
string path= System.Web.HttpContext.Current.Server.MapPath("DataBase\\ZSBDB.mdb");
constr ="provider=Microsoft.Jet.OLEDB.4.0;Data Source="+path+"";
return constr;
}
public void DBopen()
{
string constr;
constr = getconnstring();
conn = new OleDbConnection(constr);
conn.Open();
}
//关闭数据库的连接
public void DBclose()
{
conn.Close();
}
public DataSet DataSets(string query)
{
DBopen();
OleDbDataAdapter adapter = new OleDbDataAdapter(query, conn);
DataSet dtSet = new DataSet();
adapter.Fill(dtSet);
DBclose();
return dtSet;
}
public DataView Dataviews(string sql)
{
DataSet dataset = new DataSet();
dataset = DataSets(sql);
DataView dataview = new DataView(dataset.Tables[0]);
DBclose();
return dataview;
}
public SqlDataSource DataSource(string sql)
{
DBopen();
AccessDataSource datasource = new AccessDataSource(getconnstring(), sql);
DBclose();
return datasource;
}
public void sqlexecute(string sql)
{
DBopen();
OleDbCommand command = new OleDbCommand(sql, conn);
try
{
command.ExecuteNonQuery();
}
catch (OleDbException ex)
{
throw new Exception(ex.Message);
}
finally
{
DBclose();
}
}
public int sqlexecutereader(string sql)
{
DBopen();
OleDbCommand myComm = new OleDbCommand(sql, conn);
int newID = Convert.ToInt32(myComm.ExecuteScalar());
DBclose();
return newID;
}
}
说明:在访问Access数据库的时候也可把数据库访问信息放在配置文件中,在此处我是直接使用了。
SQL-Server数据库:
首先在命名空间中添加访问SQL-Server数据库的引用
using System.Data.SqlClient;
using System.Data.Sql;
**********************
public class common
{
public common()
{
//
// TODO: 在此处添加构造函数逻辑
//
}
public SqlConnection Connection;
public string getconnstring()
{
string constr;
constr = System.Configuration.ConfigurationManager.ConnectionStrings["EMISConnectionString"].ToString();
return constr;
}
public void DBopen()
{
//从Web.config中获取连接数据库字符串
string constr;
constr = getconnstring();
Connection = new SqlConnection(constr);
Connection.Open();
}
//关闭数据库的连接
public void DBclose()
{
Connection.Close();
}
public DataSet DataSets(string query)
{
DBopen();
SqlDataAdapter adapter = new SqlDataAdapter(query, Connection);
DataSet dtSet = new DataSet();
adapter.Fill(dtSet);
DBclose();
return dtSet;
}
public DataView Dataviews(string sql)
{
DataSet dataset = new DataSet();
dataset = DataSets(sql);
DataView dataview = new DataView(dataset.Tables[0]);
DBclose();
return dataview;
}
public SqlDataSource DataSource(string sql)
{
DBopen();
SqlDataSource datasource = new SqlDataSource(getconnstring(), sql);
DBclose();
return datasource;
}
public void sqlexecute(string sql)
{
DBopen();
SqlCommand command = new SqlCommand(sql, Connection);
try
{
command.ExecuteNonQuery();
}
catch (SqlException ex)
{
throw new Exception(ex.Message);
}
finally
{
DBclose();
}
}
public int sqlexecutereader(string sql)
{
DBopen();
SqlCommand myComm = new SqlCommand(sql, Connection);
int newID = Convert.ToInt32(myComm.ExecuteScalar());
DBclose();
return newID;
}
}