在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
第一次在博客园写博文,由于文采不怎么好,即使是自己很熟悉的东西,写起来也会感觉到不知从何讲起,我想写的多了就好了。 这篇文章主要是介绍怎么用c# 语言 vs2010连接access数据库的,连接字符串写到app.config配置文件中,具体的数据库操作写在一个名为sqlhelper.cs的类中,希望这篇文章能够帮助一些新手菜鸟。 1、app.config文件的代码 (1)这是连接access2007 access2010版数据库的连接字符串,其中Data Source后跟的是数据库文件的存放路径,Database Password为access数据库访问密码 1 <configuration> 2 <appSettings> 3 <add key="connstr" value="Provider=Microsoft.ACE.OLEDB.12.0;Data Source=E:\\data2\\CRMdata.accdb;Jet OLEDB:Database Password=123456" /> 4 </appSettings> 5 6 </configuration> (2)连接access2003版本数据库的连接字符串 1 <configuration> 2 <appSettings> 3 <add key="connstr" value="Provider=Microsoft.Jet.OLEDB.4.0;Data Source=E:\\data2\\CRMdata.accdb;Jet OLEDB:Database Password=123456" /> 4 </appSettings> 5 6 </configuration> 2、sqlhelper.cs文件代码 注意:要想执行oledb相关的命令必须引用,//using System.Data; //读取配置文件连接字符串 的语句中提示ConfigurationManager对象不存在时,解决办法,在资源管理器中“右击 引用 文件夹 选择添加引用,“在net选项中选中System.Configuration 单击确定即可”” 说白了就是添加引用“System.Configuration.” 1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Configuration; 6 using System.Data; 7 using System.Data.OleDb; 8 9 namespace CRMProject 10 { 11 class Sqlhelper 12 { 13 //读取配置文件连接字符串 14 public static readonly string connstr = System.Configuration.ConfigurationManager.AppSettings["connstr"].ToString(); 15 public static int ExecuteNonQuery(string cmdText, params OleDbParameter[] parameters) 16 { 17 using (OleDbConnection conn =new OleDbConnection(connstr)) 18 { 19 conn.Open(); 20 using (OleDbCommand cmd = conn.CreateCommand()) 21 { 22 cmd.CommandText = cmdText; 23 cmd.CommandTimeout = 3000; 24 cmd.Parameters.AddRange(parameters); 25 return cmd.ExecuteNonQuery(); 26 } 27 } 28 } 29 30 public static object ExecuteScalar(string cmdText,params OleDbParameter[] parameters) 31 { 32 using (OleDbConnection conn = new OleDbConnection(connstr)) 33 { 34 conn.Open(); 35 using (OleDbCommand cmd = conn.CreateCommand()) 36 { 37 cmd.CommandText = cmdText; 38 cmd.CommandTimeout = 3000; 39 cmd.Parameters.AddRange(parameters); 40 return cmd.ExecuteScalar(); 41 } 42 } 43 } 44 45 public static DataTable ExecuteDataTable(string cmdText,params OleDbParameter[] parameters) 46 { 47 using (OleDbConnection conn = new OleDbConnection(connstr)) 48 { 49 conn.Open(); 50 using (OleDbCommand cmd = conn.CreateCommand()) 51 { 52 cmd.CommandText = cmdText; 53 cmd.CommandTimeout = 3000; 54 cmd.Parameters.AddRange(parameters); 55 using (OleDbDataAdapter adapter = new OleDbDataAdapter(cmd)) 56 { 57 DataTable dt = new DataTable(); 58 adapter.Fill(dt); 59 return dt; 60 } 61 } 62 } 63 } 64 public static DataSet ExecuteDataSet(string cmdText,params OleDbParameter[] parameters) 65 { 66 using (OleDbConnection conn = new OleDbConnection(connstr)) 67 { 68 conn.Open(); 69 using (OleDbCommand cmd = conn.CreateCommand()) 70 { 71 cmd.CommandText = cmdText; 72 cmd.CommandTimeout = 3000; 73 cmd.Parameters.AddRange(parameters); 74 using (OleDbDataAdapter adapter = new OleDbDataAdapter(cmd)) 75 { 76 DataSet dt =new DataSet(); 77 adapter.Fill(dt); 78 return dt; 79 } 80 } 81 } 82 } 83 public static OleDbDataReader ExecuteDataReader(string cmdText,params OleDbParameter[] parameters) 84 { 85 OleDbConnection conn = new OleDbConnection(connstr); 86 conn.Open(); 87 using (OleDbCommand cmd = conn.CreateCommand()) 88 { 89 cmd.CommandText = cmdText; 90 cmd.CommandTimeout = 3000; 91 cmd.Parameters.AddRange(parameters); 92 return cmd.ExecuteReader(CommandBehavior.CloseConnection); 93 } 94 } 95 } 96 } 在具体的业务类中调用sqlhelper.cs中的方法即可 |
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论