数据库连接字符串
--连接Access
String conn="Data Source='fdc.mdb';Jet OLEDB:database password=123; Provider='Microsoft.Jet.OLEDB.4.0';User ID=Admin";
--连接Excel
String conn = "Provider=Microsoft.Jet.OleDb.4.0; Data Source=AllInserInfo.xls; Extended Properties=Excel 8.0;";
--连接SQL server
String conn = "Data Source=(local);Initial Catalog=BarefootIndex0425;User Id=sa;Password=sa;Connect Timeout=180;Enlist=true;Pooling=true;Max Pool Size = 300;Min Pool Size = 5;Connection Lifetime=200;packet size=4096";
-------------------------------------------------------------------
App.config/Web.config文件中配置。
--SQL连接
<add key="ConnectionString" value="server=221.2.10.100,6533;uid=myuid;pwd=mypwd;Trusted_Connection=false;database=db1;" />
<add name="MyConnection" connectionString="Data Source=192.168.1.100,2451;Initial Catalog=DefaultDbName;User ID=sa;PassWord=sa;" providerName="System.Data.SqlClient" />
--读取信息
System.Configuration.ConfigurationSettings.AppSettings["ConnectionString"].ToString();
-------------------------------------------------------------------
忘记连接字符串?试试
方法一:
新建一个文本文件,把后缀名改成*.udl
双击打开按照提示完成里面的操作
然后测试连接,成功后关闭
再用记事本打开udl文件,里面就有连接字符串了
方法二:
http://www.connectionstrings.com/
----------------------------读取excel------------------------------
![](https://images.cnblogs.com/OutliningIndicators/ExpandedBlockStart.gif)
1 string excelconnstring = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=D:\\a.xsl;Extended Properties=""Excel 8.0;HDR=Yes;IMEX=1""";
2 OleDbConnection excelconn = new OleDbConnection(excelconnstring);
3 excelconn.Open();
4
5 DataTable sheets = excelconn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, new object[] { null, null, null, "TABLE" });
6 foreach(DataRow row in sheets.Rows)
7 {
8 tableNames.Add(row["TABLE_NAME"]); //读取表名
9 }
10 foreach(string tbName in tableNames)
11 {
12 string sql = string.Format("select * from [{0}]", tbName);
13 OleDbDataAdapter mycomm = new OleDbDataAdapter(sql, excelconn);
14 DataSet myds = new DataSet();
15 mycomm.Fill(myds, toTable);
16 }
17
-------------------------config文件两种配置示例--------------
<!-- App.config, Web.config 应用程序配置文件 -->
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<appSettings>
<add key="ServiceName" value="MSSQLSERVER"/> <!-- SQL2005 "MSSQL$SQLEXPRESS"-->
</appSettings>
<connectionStrings>
<add name="SQL2000" connectionString="Data Source=.; DataBase=ProductSet; Integrated Security=True;" />
</connectionStrings>
<!-- SQL 2005, 2008
<connectionStrings>
<add name="DSN" connectionString="Data Source=.\SQLEXPRESS; AttachDBFilename=|DataDirectory|\ProductSet.mdf; Integrated Security=True; User Instance=True"/>
</connectionStrings>
<connectionStrings>
<add name="Access2003" connectionString="Provider=Microsoft.Jet.OLEDB.4.0;Data Source=|DataDirectory|\Image.mdb;Jet OLEDB:Database Password=jinzhexian"
providerName="System.Data.OleDb" />
<add name="Access2007" connectionString="Provider=Microsoft.ACE.OLEDB.12.0;Data Source=|DataDirectory|\User.accdb;Jet OLEDB:Database Password=jinzhexian"
providerName="System.Data.OleDb" />
</connectionStrings>
-->
</configuration>
//读取:
using System.Configuration;
string sql = ConfigurationManager.ConnectionStrings["DSN"].ConnectionString;
string serviceName = ConfigurationManager.AppSettings["ServiceName"];
配置系统未能初始化 错误的解决方案
今天修改了App.config,结果运行的时候出现了 "配置系统未能初始化" 的错误.找了半天才发现是下面的原因造成的:
MSDN里写到"如果配置文件中包含 configSections 元素,则 configSections 元素必须是 configuration 元素的第一个子元素。".因为我把appSettings写它前面了..所以才出这样的错.