龍巖易富通網(wǎng)絡(luò)科技有限公司

龍巖小程序開(kāi)發(fā),龍巖分銷(xiāo)系統(tǒng)

c#—winForm連接sql--App.config

2015.09.10 | 456閱讀 | 0條評(píng)論 | 未命名

c#—winForm連接sql 1、首先創(chuàng)建App.config文件,例如: <?xml version="1.0" encoding="utf-8" ?> <configuration> <appSettings > <add key ="Default" value ="Default"/>?? //先添加key,value <add key ="Access" value ="Access"/> </appSettings> <connectionStrings > <add name="Default" connectionString="Data Source=.;User ID=sa;Password=sa;Initial Catalog=HHHH" providerName="System.Data.SqlClient" /> <add name="Access" connectionString="Provider=Microsoft.Jet.OLEDB.4.0;Data Source=|DataDirectory|\aaaa.mdb" providerName="System.Data.OleDb" /> </connectionStrings> </configuration> 2、 (1).SQL獲取數(shù)據(jù)庫(kù)字符串代碼: string strCon = ConfigurationManager.ConnectionStrings["Default"].ToString(); (2).OleDb connectionSettings = ConfigurationManager.ConnectionStrings[connectionStrings]; DbProviderFactory provider = DbProviderFactories.GetFactory(connectionSettings.ProviderName); 3、string strCon = ConfigurationManager.ConnectionStrings["Default"].ToString(); SqlConnection con = new SqlConnection(); con.ConnectionString = strCon; con.Open(); string sql = "select aa as ID from taba"; SqlDataAdapter da = new SqlDataAdapter(sql, con); DataSet ds = new DataSet(); da.Fill(ds); //填充DataSet DataTable dt = ds.Tables[0]; ArrayList a = new ArrayList(); for (int i = 0; i < dt.Rows.Count; i++) { a.Add(dt.Rows[i][0].ToString()); }

贊 (

發(fā)表評(píng)論