数据库配置[z]

c#—winForm连接sql
1、首先创建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获取数据库字符串代码:
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());
}

你可能感兴趣的:(数据库)