电话记录管理的制作1(连接数据库的方法们)
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 class Sdatareader 2 { 3 //ermethod方法是reader的方法,这里的commandbehavior.closeconnection会自动关闭连接和using的作用一样 4 public static OleDbDataReader ermethod(string str, params OleDbParameter[] parameters) 5 { 6 String connstr = ConfigurationManager.ConnectionStrings["Tdatabase"].ConnectionString; 7 OleDbConnection conn = new OleDbConnection(connstr); 8 9 conn.Open(); 10 OleDbCommand com = conn.CreateCommand(); 11 com.CommandText = str; 12 foreach (OleDbParameter parameter in parameters) 13 { 14 com.Parameters.Add(parameter); 15 } 16 OleDbDataReader reader = com.ExecuteReader(CommandBehavior.CloseConnection); 17 return reader; 18 } 19 //将运行得出的表放入一张table中,似乎没有什么用而且还不能断开连接 20 public static DataTable dtmethod(string str, params OleDbParameter[] parameters) 21 { 22 String connstr = ConfigurationManager.ConnectionStrings["Tdatabase"].ConnectionString; 23 OleDbConnection conn = new OleDbConnection(connstr); 24 25 conn.Open(); 26 OleDbCommand com = conn.CreateCommand(); 27 com.CommandText = str; 28 foreach (OleDbParameter parameter in parameters) 29 { 30 com.Parameters.Add(parameter); 31 } 32 OleDbDataAdapter adapter = new OleDbDataAdapter(com); 33 DataTable dt = new DataTable(); 34 adapter.Fill(dt); 35 return dt; 36 } 37 //将运行得出的表放入一个table集中,似乎没有什么用而且还不能断开连接 38 public static DataSet dsmethod(string str, params OleDbParameter[] parameters) 39 { 40 String connstr = ConfigurationManager.ConnectionStrings["Tdatabase"].ConnectionString; 41 OleDbConnection conn = new OleDbConnection(connstr); 42 43 conn.Open(); 44 OleDbCommand com = conn.CreateCommand(); 45 com.CommandText = str; 46 foreach (OleDbParameter parameter in parameters) 47 { 48 com.Parameters.Add(parameter); 49 } 50 DataSet ds = new DataSet(); 51 OleDbDataAdapter ad = new OleDbDataAdapter(com); 52 ad.Fill(ds); 53 return ds; 54 } 55 //最常用的方法查询、删除、更新、增加 56 public static void enqmethod(string str, params OleDbParameter[] parameters) 57 { 58 String connstr = ConfigurationManager.ConnectionStrings["Tdatabase"].ConnectionString; 59 using (OleDbConnection conn = new OleDbConnection(connstr)) 60 { 61 conn.Open(); 62 using (OleDbCommand com = conn.CreateCommand()) 63 { 64 com.CommandText = str; 65 foreach (OleDbParameter parameter in parameters) 66 { 67 com.Parameters.Add(parameter); 68 } 69 int I = com.ExecuteNonQuery(); 70 } 71 } 72 } 73 //不需要的东西因为之前的任意个params包含了没有的情况 74 public static void enqmethod(string str) 75 { 76 String connstr = ConfigurationManager.ConnectionStrings["Tdatabase"].ConnectionString; 77 using (OleDbConnection conn = new OleDbConnection(connstr)) 78 { 79 conn.Open(); 80 using (OleDbCommand com = conn.CreateCommand()) 81 { 82 com.CommandText = str; 83 84 int I = com.ExecuteNonQuery(); 85 } 86 } 87 } 88 //返回执行结果中第一行第一列的值 89 public static string esmethod(string str, params OleDbParameter[] parameters) 90 { 91 String connstr = ConfigurationManager.ConnectionStrings["Tdatabase"].ConnectionString; 92 using (OleDbConnection conn = new OleDbConnection(connstr)) 93 { 94 conn.Open(); 95 using (OleDbCommand com = conn.CreateCommand()) 96 { 97 com.CommandText = str; 98 foreach (OleDbParameter parameter in parameters) 99 { 100 com.Parameters.Add(parameter); 101 } 102 return com.ExecuteScalar().ToString(); 103 } 104 } 105 } 106 }
这些方法用于oledb中,sql的话要做少量修改
并且想要顺利执行还要加上
using System.Configuration;
using System.Data.OleDb;
using System.Data;
using System.Collections;
这几条代码
并且在引用中添加
组件名称:system.configuration
类型:.NET