spring.net对ado.net进行的封装,使我们更加轻松的访问数据库。
先看例子:
view plaincopy to clipboardprint?
using System;
using Spring.Data.Core;
using Spring.Data.Common;
namespace data_test
{
class Program
{
static void Main(string[] args)
{
IDbProvider idbp = DbProviderFactory.GetDbProvider("System.Data.SqlClient");
idbp.ConnectionString = "Data Source=localhost;Initial Catalog=test;Integrated Security=True";
AdoTemplate at = new Spring.Data.Core.AdoTemplate(idbp);
at.ExecuteNonQuery(System.Data.CommandType.Text, "insert table1 values(2)");
}
}
}
using System;
using Spring.Data.Core;
using Spring.Data.Common;
namespace data_test
{
class Program
{
static void Main(string[] args)
{
IDbProvider idbp = DbProviderFactory.GetDbProvider("System.Data.SqlClient");
idbp.ConnectionString = "Data Source=localhost;Initial Catalog=test;Integrated Security=True";
AdoTemplate at = new Spring.Data.Core.AdoTemplate(idbp);
at.ExecuteNonQuery(System.Data.CommandType.Text, "insert table1 values(2)");
}
}
}
在这个例子中,我们执行的一个sql语句"insert table1 values(2)",下面我们具体分析一下:
IDbProvider idbp = DbProviderFactory.GetDbProvider("System.Data.SqlClient");
idbp.ConnectionString = "Data Source=localhost;Initial Catalog=test;Integrated Security=Tue";
这两句我们通过DbProviderFactory工厂建立了一个IDbProvider 对象,但实际的数据库访问时通过"System.Data.SqlClient"完成的。也就是说当我们建立了一个IDbProvider 对象时,系统实例化了个System.Data.SqlClient对象。
AdoTemplate at = new Spring.Data.Core.AdoTemplate(idbp);
AdoTemplate 是把典型的“样板式”任务封装起来。
at.ExecuteNonQuery(System.Data.CommandType.Text, "insert table1 values(2)");
执行sql语句 。
不过,spring.net中怎么能这么写代码,当然是用配置文件搞定。 我们直接我上面的代码翻译成配置文件:
view plaincopy to clipboardprint?
<object id ="DbProvider" type ="Spring.Data.Common.DbProviderFactory,Spring.Data"
factory-method ="GetDbProvider" >
<constructor-arg name ="providerInvariantName" value ="System.Data.SqlClient"></constructor-arg>
<property name ="ConnectionString" value ="Data
Source=localhost;Initial Catalog=test;Integrated
Security=True"></property>
</object>
<object id="adoTemplate" type="Spring.Data.Core.AdoTemplate, Spring.Data">
<property name="DbProvider" ref="DbProvider"/>
</object>
<object id ="DbProvider" type ="Spring.Data.Common.DbProviderFactory,Spring.Data"
factory-method ="GetDbProvider" >
<constructor-arg name ="providerInvariantName" value ="System.Data.SqlClient"></constructor-arg>
<property name ="ConnectionString" value ="Data
Source=localhost;Initial Catalog=test;Integrated
Security=True"></property>
</object>
<object id="adoTemplate" type="Spring.Data.Core.AdoTemplate, Spring.Data">
<property name="DbProvider" ref="DbProvider"/>
</object>
调用代码:
view plaincopy to clipboardprint?
//取得容器
IApplicationContext ioc = ContextRegistry.GetContext();
//取得AdoTemplate对象
AdoTemplate at = (AdoTemplate)ioc.GetObject("adoTemplate");
//执行sql语句
at.ExecuteNonQuery(System.Data.CommandType.Text, "insert table1 values(2)");
//取得容器
IApplicationContext ioc = ContextRegistry.GetContext();
//取得AdoTemplate对象
AdoTemplate at = (AdoTemplate)ioc.GetObject("adoTemplate");
//执行sql语句
at.ExecuteNonQuery(System.Data.CommandType.Text, "insert table1 values(2)");
当然,你也可以用:
view plaincopy to clipboardprint?
<objects xmlns='http://www.springframework.net'
xmlns:d="http://www.springframework.net/database"> <d:dbProvider
id="DbProvider" provider="System.Data.SqlClient" connectionString="Data
Source=(local);Database=Spring;User
ID=springqa;Password=springqa;Trusted_Connection=False"/> <object
id="adoTemplate" type="Spring.Data.AdoTemplate, Spring.Data">
<property name="DbProvider" ref="DbProvider"/> </object>
</objects>