在C#的学习中,操作数据库是比较常用的技术,而access和sql server 数据库的操作却有着不同。那么,有哪些不同呢?
首先,需要引用不同的类。因为有着不同的数据引擎。
access:using System.Data.OleDb;
sql server:using System.Data.SqlClient;
下面是我写的access和sql 数据库操作的两个类:
//1、操作sql数据库的类
public class DBOperSQL1
{
public static string mark = "sql";
//连接对象私有字段
private static SqlConnection conn;
//连接对象的属性
public static SqlConnection Connection
{
get
{
if (conn == null)
{
//从配置文件中获取连接字符串,取消
string connStr = ConfigurationManager.ConnectionStrings["medicineConStr"].ConnectionString;
conn = new SqlConnection(connStr);
conn.Open();
}
else if (conn.State == System.Data.ConnectionState.Closed)
{
conn.Open();
}
else if (conn.State == System.Data.ConnectionState.Broken)
{
conn.Close();
conn.Open();
}
return conn;
}
} //不带参数的执行
public static int ExecuteCommand(string sql)
{
SqlCommand cmd = new SqlCommand(sql, Connection);
return cmd.ExecuteNonQuery();
} //带参数的执行
public static int ExecuteCommand(string sql, params SqlParameter[] values)
{
SqlCommand cmd = new SqlCommand(sql, Connection);
cmd.Parameters.AddRange(values);
return cmd.ExecuteNonQuery(); } //不带参数的获取数据读取器
public static SqlDataReader GetReader(string sql)
{
SqlCommand cmd = new SqlCommand(sql, Connection);
return cmd.ExecuteReader();
}
//带参数的数据读取器
public static SqlDataReader GetReader(string sql, params SqlParameter[] values)
{
SqlCommand cmd = new SqlCommand(sql, Connection);
cmd.Parameters.AddRange(values);
return cmd.ExecuteReader();
} //不带参数的获取数据数据集中的表
public static DataTable GetDataTable(string sql)
{
SqlDataAdapter da = new SqlDataAdapter(sql, Connection);
DataSet ds = new DataSet();
da.Fill(ds);
return ds.Tables[];
}
//带参数的数据集中的表
public static DataTable GetDataTable(string sql, params SqlParameter[] values)
{
SqlCommand cmd = new SqlCommand(sql, Connection);
cmd.Parameters.AddRange(values);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds);
return ds.Tables[];
}
}
2.操作access数据库的类
//操作access
public class DBOperAccess
{
public static string mark = "access";
//连接对象私有字段
private static OleDbConnection conn;
//连接对象的属性
public static OleDbConnection Connection
{
get
{
if (conn == null)
{
//从配置文件中获取连接字符串,取消
string connStr = ConfigurationManager.ConnectionStrings["medConStr"].ConnectionString;
//string connStr= "Provider=Microsoft.Jet.OLEDB.4.0 ;Data Source=D:\\med\\medicine.mdb";
conn = new OleDbConnection(connStr);
conn.Open();
}
else if (conn.State == System.Data.ConnectionState.Closed)
{
conn.Open();
}
else if (conn.State == System.Data.ConnectionState.Broken)
{
conn.Close();
conn.Open();
}
return conn;
}
} //不带参数的执行
public static int ExecuteCommand(string sql)
{
OleDbCommand cmd = new OleDbCommand(sql, Connection);
return cmd.ExecuteNonQuery();
} //带参数的执行
public static int ExecuteCommand(string sql, params OleDbParameter[] values)
{
OleDbCommand cmd = new OleDbCommand(sql, Connection);
cmd.Parameters.AddRange(values);
return cmd.ExecuteNonQuery(); } //不带参数的获取数据读取器
public static OleDbDataReader GetReader(string sql)
{
OleDbCommand cmd = new OleDbCommand(sql, Connection);
return cmd.ExecuteReader();
}
//带参数的数据读取器
public static OleDbDataReader GetReader(string sql, params OleDbParameter[] values)
{
OleDbCommand cmd = new OleDbCommand(sql, Connection);
cmd.Parameters.AddRange(values);
return cmd.ExecuteReader();
} //不带参数的获取数据数据集中的表
public static DataTable GetDataTable(string sql)
{
OleDbDataAdapter da = new OleDbDataAdapter(sql, Connection);
DataSet ds = new DataSet();
da.Fill(ds);
return ds.Tables[];
}
//带参数的数据集中的表
public static DataTable GetDataTable(string sql, params OleDbParameter[] values)
{
OleDbCommand cmd = new OleDbCommand(sql, Connection);
cmd.Parameters.AddRange(values);
OleDbDataAdapter da = new OleDbDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds);
return ds.Tables[];
}
}