数据库连接字符串
时间:2010-09-30 来源:djian
--连接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=219.136.253.61,6533;uid=www163ebay;pwd=Kingdom_www_pop163;Trusted_Connection=false;database=163ebay;" />
<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------------------------------
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