首页 > 代码库 > C#操作access和SQL server数据库代码实例
C#操作access和SQL server数据库代码实例
在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 get11 {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 get11 {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 }
C#操作access和SQL server数据库代码实例
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。