![ContractedBlock.gif](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![ExpandedBlockStart.gif](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
///
在使用本例之前要先手动引用system.configration组件,然后建立config文件,config文件名为当前程序文件名的后面加.config比如:a.exe.config
using System;
using System.Collections.Generic;
using System.Text;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Data.Common;
namespace ConsoleApplication313
{
class Program
{
static void Main( string [] args)
{
ConnectionStringSettings cfg = ConfigurationManager.ConnectionStrings[ " My DB " ];
DbProviderFactory factory = DbProviderFactories.GetFactory(cfg.ProviderName);
DbConnection cnx = factory.CreateConnection();
DbCommand cmd = factory.CreateCommand();
cnx.ConnectionString = cfg.ConnectionString;
cmd.Connection = cnx;
cnx.Open();
cmd.CommandText = string .Format( " select * from student " );
DbDataReader dbdatareader =
cmd.ExecuteReader();
while (dbdatareader.Read())
{
Console.Write( string .Format( " {0} {1} {2} {3} {4} {5} " ,dbdatareader[ 0 ],dbdatareader[ 1 ],
dbdatareader[ 2 ],dbdatareader[ 3 ],dbdatareader[ 4 ],dbdatareader[ 5 ]));
Console.WriteLine();
}
System.Console.ReadLine();
}
}
}
using System;
using System.Collections.Generic;
using System.Text;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Data.Common;
namespace ConsoleApplication313
{
class Program
{
static void Main( string [] args)
{
ConnectionStringSettings cfg = ConfigurationManager.ConnectionStrings[ " My DB " ];
DbProviderFactory factory = DbProviderFactories.GetFactory(cfg.ProviderName);
DbConnection cnx = factory.CreateConnection();
DbCommand cmd = factory.CreateCommand();
cnx.ConnectionString = cfg.ConnectionString;
cmd.Connection = cnx;
cnx.Open();
cmd.CommandText = string .Format( " select * from student " );
DbDataReader dbdatareader =
cmd.ExecuteReader();
while (dbdatareader.Read())
{
Console.Write( string .Format( " {0} {1} {2} {3} {4} {5} " ,dbdatareader[ 0 ],dbdatareader[ 1 ],
dbdatareader[ 2 ],dbdatareader[ 3 ],dbdatareader[ 4 ],dbdatareader[ 5 ]));
Console.WriteLine();
}
System.Console.ReadLine();
}
}
}
下面是XML文件的内容:
![ContractedBlock.gif](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![ExpandedBlockStart.gif](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
<?
xml version="1.0" encoding="utf-8"
?>
< configuration >
< connectionStrings >
< add name ="My DB" providerName ="System.Data.SqlClient" connectionString ="server=.;uid=sa;pwd=sqlserver;database=master" />
</ connectionStrings >
</ configuration >
< configuration >
< connectionStrings >
< add name ="My DB" providerName ="System.Data.SqlClient" connectionString ="server=.;uid=sa;pwd=sqlserver;database=master" />
</ connectionStrings >
</ configuration >