在C#的学习中,操作数据库是比较常用的技术,而access和sql server 数据库的操作却有着不同。那么,有哪些不同呢?
首先,需要引用不同的类。因为有着不同的数据引擎。
access:using System.Data.OleDb;
sql server:using System.Data.SqlClient;
下面是我写的access和sql 数据库操作的两个类:
1 //1、操作sql数据库的类 2 public class DBOperSQL1 3 { 4 public static string mark = "sql"; 5 //连接对象私有字段 6 private static SqlConnection conn; 7 //连接对象的属性 8 public static SqlConnection Connection 9 { 10 get 11 { 12 if (conn == null) 13 { 14 //从配置文件中获取连接字符串,取消 15 string connStr = ConfigurationManager.ConnectionStrings["medicineConStr"].ConnectionString; 16 conn = new SqlConnection(connStr); 17 conn.Open(); 18 } 19 else if (conn.State == System.Data.ConnectionState.Closed) 20 { 21 conn.Open(); 22 } 23 else if (conn.State == System.Data.ConnectionState.Broken) 24 { 25 conn.Close(); 26 conn.Open(); 27 } 28 return conn; 29 } 30 } 31 32 //不带参数的执行 33 public static int ExecuteCommand(string sql) 34 { 35 SqlCommand cmd = new SqlCommand(sql, Connection); 36 return cmd.ExecuteNonQuery(); 37 } 38 39 //带参数的执行 40 public static int ExecuteCommand(string sql, params SqlParameter[] values) 41 { 42 SqlCommand cmd = new SqlCommand(sql, Connection); 43 cmd.Parameters.AddRange(values); 44 return cmd.ExecuteNonQuery(); 45 46 } 47 48 //不带参数的获取数据读取器 49 public static SqlDataReader GetReader(string sql) 50 { 51 SqlCommand cmd = new SqlCommand(sql, Connection); 52 return cmd.ExecuteReader(); 53 } 54 //带参数的数据读取器 55 public static SqlDataReader GetReader(string sql, params SqlParameter[] values) 56 { 57 SqlCommand cmd = new SqlCommand(sql, Connection); 58 cmd.Parameters.AddRange(values); 59 return cmd.ExecuteReader(); 60 } 61 62 //不带参数的获取数据数据集中的表 63 public static DataTable GetDataTable(string sql) 64 { 65 SqlDataAdapter da = new SqlDataAdapter(sql, Connection); 66 DataSet ds = new DataSet(); 67 da.Fill(ds); 68 return ds.Tables[0]; 69 } 70 //带参数的数据集中的表 71 public static DataTable GetDataTable(string sql, params SqlParameter[] values) 72 { 73 SqlCommand cmd = new SqlCommand(sql, Connection); 74 cmd.Parameters.AddRange(values); 75 SqlDataAdapter da = new SqlDataAdapter(cmd); 76 DataSet ds = new DataSet(); 77 da.Fill(ds); 78 return ds.Tables[0]; 79 } 80 }
2.操作access数据库的类
1 //操作access 2 public class DBOperAccess 3 { 4 public static string mark = "access"; 5 //连接对象私有字段 6 private static OleDbConnection conn; 7 //连接对象的属性 8 public static OleDbConnection Connection 9 { 10 get 11 { 12 if (conn == null) 13 { 14 //从配置文件中获取连接字符串,取消 15 string connStr = ConfigurationManager.ConnectionStrings["medConStr"].ConnectionString; 16 //string connStr= "Provider=Microsoft.Jet.OLEDB.4.0 ;Data Source=D:\med\medicine.mdb"; 17 conn = new OleDbConnection(connStr); 18 conn.Open(); 19 } 20 else if (conn.State == System.Data.ConnectionState.Closed) 21 { 22 conn.Open(); 23 } 24 else if (conn.State == System.Data.ConnectionState.Broken) 25 { 26 conn.Close(); 27 conn.Open(); 28 } 29 return conn; 30 } 31 } 32 33 //不带参数的执行 34 public static int ExecuteCommand(string sql) 35 { 36 OleDbCommand cmd = new OleDbCommand(sql, Connection); 37 return cmd.ExecuteNonQuery(); 38 } 39 40 //带参数的执行 41 public static int ExecuteCommand(string sql, params OleDbParameter[] values) 42 { 43 OleDbCommand cmd = new OleDbCommand(sql, Connection); 44 cmd.Parameters.AddRange(values); 45 return cmd.ExecuteNonQuery(); 46 47 } 48 49 //不带参数的获取数据读取器 50 public static OleDbDataReader GetReader(string sql) 51 { 52 OleDbCommand cmd = new OleDbCommand(sql, Connection); 53 return cmd.ExecuteReader(); 54 } 55 //带参数的数据读取器 56 public static OleDbDataReader GetReader(string sql, params OleDbParameter[] values) 57 { 58 OleDbCommand cmd = new OleDbCommand(sql, Connection); 59 cmd.Parameters.AddRange(values); 60 return cmd.ExecuteReader(); 61 } 62 63 //不带参数的获取数据数据集中的表 64 public static DataTable GetDataTable(string sql) 65 { 66 OleDbDataAdapter da = new OleDbDataAdapter(sql, Connection); 67 DataSet ds = new DataSet(); 68 da.Fill(ds); 69 return ds.Tables[0]; 70 } 71 //带参数的数据集中的表 72 public static DataTable GetDataTable(string sql, params OleDbParameter[] values) 73 { 74 OleDbCommand cmd = new OleDbCommand(sql, Connection); 75 cmd.Parameters.AddRange(values); 76 OleDbDataAdapter da = new OleDbDataAdapter(cmd); 77 DataSet ds = new DataSet(); 78 da.Fill(ds); 79 return ds.Tables[0]; 80 } 81 }