做C#.net 项目,如何使用app.config 文件来连接数据库,而不是反复的使用数据库连接代码(数据库为Access)
我用的可见化模式配置添加的数据源,因为产生了app.config配置文件app.config中如下:<?xmlversion="1.0"encoding="utf-8"?...
我用的可见化模式配置添加的数据源,因为产生了app.config 配置文件
app.config 中如下:<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
</configSections>
<connectionStrings>
<add name="ButterflyFinance.Properties.Settings.ButterflyConnectionString"
connectionString="Provider=Microsoft.Jet.OLEDB.4.0;Data Source=|DataDirectory|\Butterfly.mdb"
providerName="System.Data.OleDb" />
</connectionStrings>
</configuration>
我想问下,具体如何使用来替代连接数据库语句,比如如何替代以下语句:
String ConStr ="Provider=Microsoft.Jet.OLEDB.4.0;Data Source=Butterfly.mdb";
OleDbConnection oc = new OleDbConnection(ConStr);
oc.Open();
OleDbCommand dataCommand = new OleDbCommand();
dataCommand.Connection = oc;
dataCommand.CommandText = "select TOP 1 * from [User] where UserName = '" + UserName1 + "' and Password ='" + Password1 + "' ";
OleDbDataReader dr = dataCommand.ExecuteReader();
现经过试验把各位答案修正总结如下:
1。首先项目必须添加引用 System.Configuration
2。在本窗体 最前面 添加 using System.Configuration;
3。在代码这样替代String ConStr=ConfigurationManager.ConnectionStrings["ButterflyFinance.Properties.Settings.ButterflyConnectionString"].ConnectionString
或者String ConStr=System.Configuration.ConfigurationManager.ConnectionStrings["ButterflyFinance.Properties.Settings.ButterflyConnectionString"].ConnectionString
4。"ButterflyFinance.Properties.Settings.ButterflyConnectionString"可以在app.config中修改名称为ButterflyConnectionString 因此3。中代码可以简略为
String ConStr=ConfigurationManager.ConnectionStrings["ButterflyConnectionString"].ConnectionString 展开
app.config 中如下:<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
</configSections>
<connectionStrings>
<add name="ButterflyFinance.Properties.Settings.ButterflyConnectionString"
connectionString="Provider=Microsoft.Jet.OLEDB.4.0;Data Source=|DataDirectory|\Butterfly.mdb"
providerName="System.Data.OleDb" />
</connectionStrings>
</configuration>
我想问下,具体如何使用来替代连接数据库语句,比如如何替代以下语句:
String ConStr ="Provider=Microsoft.Jet.OLEDB.4.0;Data Source=Butterfly.mdb";
OleDbConnection oc = new OleDbConnection(ConStr);
oc.Open();
OleDbCommand dataCommand = new OleDbCommand();
dataCommand.Connection = oc;
dataCommand.CommandText = "select TOP 1 * from [User] where UserName = '" + UserName1 + "' and Password ='" + Password1 + "' ";
OleDbDataReader dr = dataCommand.ExecuteReader();
现经过试验把各位答案修正总结如下:
1。首先项目必须添加引用 System.Configuration
2。在本窗体 最前面 添加 using System.Configuration;
3。在代码这样替代String ConStr=ConfigurationManager.ConnectionStrings["ButterflyFinance.Properties.Settings.ButterflyConnectionString"].ConnectionString
或者String ConStr=System.Configuration.ConfigurationManager.ConnectionStrings["ButterflyFinance.Properties.Settings.ButterflyConnectionString"].ConnectionString
4。"ButterflyFinance.Properties.Settings.ButterflyConnectionString"可以在app.config中修改名称为ButterflyConnectionString 因此3。中代码可以简略为
String ConStr=ConfigurationManager.ConnectionStrings["ButterflyConnectionString"].ConnectionString 展开
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询