《C#与.NET 3.0高级程序设计》第22章读书笔记
第22章:使用 ADO.NET 访问数据库
以下的读书笔记记了该章节 .NET 2.0 ADO.NET中新增的数据提供器工厂模式的内容。
22.7.2节:完整的数据提供器的例子
这一章节是用 Pubs,本人换用 SQL Server 2005 中示例数据库: AdventureWorks重写程序代码:
app.config部分:
Code
<appSettings>
<add key="provider" value="System.Data.SqlClient" />
<add key="cnStr" value="Data Source=localhost;uid=sa;pwd=;Initial Catalog=AdventureWorks"/>
</appSettings>
<appSettings>
<add key="provider" value="System.Data.SqlClient" />
<add key="cnStr" value="Data Source=localhost;uid=sa;pwd=;Initial Catalog=AdventureWorks"/>
</appSettings>
Main()方法部分:
Code
static void Main(string[] args)
{
//从config文件中获取连接字符串和提供器
string dp = ConfigurationManager.AppSettings["provider"];
string cnStr = ConfigurationManager.AppSettings["cnStr"];
//得到工厂提供器
DbProviderFactory df = DbProviderFactories.GetFactory(dp);
//得到连接对象
DbConnection conn = df.CreateConnection();
Console.WriteLine("Your connection object is a: {0}", conn.GetType().FullName);
conn.ConnectionString = cnStr;
conn.Open();
//得到命令对象
DbCommand cmd = df.CreateCommand();
Console.WriteLine("Your command object is a: {0}", cmd.GetType().FullName);
cmd.Connection = conn;
cmd.CommandText = "Select * From Purchasing.Vendor";
//从数据读取器输出数据
DbDataReader dr = cmd.ExecuteReader(CommandBehavior.CloseConnection);
Console.WriteLine("Your data reader object is a: {0}", dr.GetType().FullName);
Console.WriteLine("\n*****Purchasing.Vendor in AdventureWorks");
while (dr.Read())
Console.WriteLine("-> {0}", dr["Name"]);
dr.Close();
}
static void Main(string[] args)
{
//从config文件中获取连接字符串和提供器
string dp = ConfigurationManager.AppSettings["provider"];
string cnStr = ConfigurationManager.AppSettings["cnStr"];
//得到工厂提供器
DbProviderFactory df = DbProviderFactories.GetFactory(dp);
//得到连接对象
DbConnection conn = df.CreateConnection();
Console.WriteLine("Your connection object is a: {0}", conn.GetType().FullName);
conn.ConnectionString = cnStr;
conn.Open();
//得到命令对象
DbCommand cmd = df.CreateCommand();
Console.WriteLine("Your command object is a: {0}", cmd.GetType().FullName);
cmd.Connection = conn;
cmd.CommandText = "Select * From Purchasing.Vendor";
//从数据读取器输出数据
DbDataReader dr = cmd.ExecuteReader(CommandBehavior.CloseConnection);
Console.WriteLine("Your data reader object is a: {0}", dr.GetType().FullName);
Console.WriteLine("\n*****Purchasing.Vendor in AdventureWorks");
while (dr.Read())
Console.WriteLine("-> {0}", dr["Name"]);
dr.Close();
}
程序使用微软SQL Server 提供器来从 AdventureWorks 数据库的 Purchasing.Vendor 表中读取数据:
22.8节:<connentionStrings>元素
app.config文件修改部分:
Code
<connectionStrings>
<add name="SqlProviderAdventureWorks" connectionString="Data Source=localhost;uid=sa;pwd=;Initial Catalog=AdventureWorks" providerName="System.Data.SqlClient"/>
</connectionStrings>
<connectionStrings>
<add name="SqlProviderAdventureWorks" connectionString="Data Source=localhost;uid=sa;pwd=;Initial Catalog=AdventureWorks" providerName="System.Data.SqlClient"/>
</connectionStrings>
Main()方法修改部分:
Code
string dp = ConfigurationManager.ConnectionStrings["SqlProviderAdventureWorks"].ProviderName;
string cnStr = ConfigurationManager.ConnectionStrings["SqlProviderAdventureWorks"].ConnectionString;
string dp = ConfigurationManager.ConnectionStrings["SqlProviderAdventureWorks"].ProviderName;
string cnStr = ConfigurationManager.ConnectionStrings["SqlProviderAdventureWorks"].ConnectionString;
22.9节:安装 Cars 数据库
Cars.sql 脚本文件在 SQL Server 2005 中运行时需要修改初始大小值。
注意粗体的部分:
Code
CREATE DATABASE [Cars] ON (NAME = N'Cars_Data', FILENAME =N'C:\Program Files\Microsoft SQL Server\MSSQL\Data\Cars_Data.MDF' , SIZE = 3, FILEGROWTH = 10%)
CREATE DATABASE [Cars] ON (NAME = N'Cars_Data', FILENAME =N'C:\Program Files\Microsoft SQL Server\MSSQL\Data\Cars_Data.MDF' , SIZE = 3, FILEGROWTH = 10%)
作者在书中是按 SQL Server 2000 创建数据库初始大小设置为2M的脚本。
本书资源: