代码改变世界

Spring.NET 無法找到數據庫連接(Could not get ADO.NET connection.)--困惑中

2011-12-08 09:51  Ref Tian  阅读(1745)  评论(1编辑  收藏  举报

最近在使用SPRING.NET的原生數據接口實現查詢功能在獲取IDbProvider 時總是提示:Could not get ADO.NET connection. 查看代碼感覺是沒有進行數據注入的原因但是具體問題出現在那裡還沒解決,配置和實現代碼如下:

  <db:provider id="sqlConnection" provider="SqlServerCe-3.5.1" connectionString="Data Source=10.194.16.126;Initial Catalog=efox;Persist Security Info=True;User ID=r;Password=1981429*a;Trusted_Connection=False"/>

<object id="adoTemplate" type="Spring.Data.Core.AdoTemplate, Spring.Data">
<property name="DbProvider" ref="sqlConnection"/>
<property name="DataReaderWrapperType" value="Spring.Data.Support.NullMappingDataReader, Spring.Data"/>
</object
  <parsers>
<parser type="Spring.Remoting.Config.RemotingNamespaceParser, Spring.Services" />
<parser type="Spring.Data.Config.DatabaseNamespaceParser, Spring.Data" />
</parsers>

<context>
<!--<resource uri="assembly://ServerMain/ServerMain.config/DbProvider.xml"/>-->
<resource uri="assembly://ServerMain/ServerMain.config/Components.xml"/><!--配置注入的容器-->
<resource uri="config://spring/objects" />
</context>


<objects xmlns="http://www.springframework.net" xmlns:r="http://www.springframework.net/remoting">
<!--這個是必須的-->
</objects>

調用代碼:

            IDbProvider dbProvider = DbProviderFactory.GetDbProvider("SqlServerCe-3.5.1");
//dbProvider.ConnectionString = "Data Source=10.194.18.158;Initial Catalog=efox;Persist Security Info=True;User ID=efox;Password=efox$88;Trusted_Connection=False";
AdoTemplate adoTemplate=new AdoTemplate(dbProvider);
int i= adoTemplate.Execute<int>(delegate(DbCommand dbCommand) {
dbCommand.CommandText =
"select count(*) from dbo.mfworkdetail(nolock) where workorderno='001400000005' ";
return (int)dbCommand.ExecuteScalar();
});

如果不去給connectionstring賦值程式就會報出上面的錯誤。郁闷中啊............

下边是通过注入获得数据的方法,执行没问题

<?xml version="1.0" encoding="utf-8" ?>
<objects xmlns="http://www.springframework.net"
xmlns:db="http://www.springframework.net/database">

<db:provider id="dbProvider"
provider="System.Data.SqlClient"
connectionString="Data Source=10.194.18.158;Database=efox;User ID=efox;Password=efox$88;Trusted_Connection=False"/>

<object id="adoTemplate" type="Spring.Data.Core.AdoTemplate, Spring.Data">
<property name="DbProvider" ref="dbProvider"/>
</object>



</objects>
<?xml version="1.0" encoding="utf-8" ?>
<configuration>

<configSections>
<sectionGroup name="spring">
<section name="context" type="Spring.Context.Support.ContextHandler, Spring.Core" />
<section name="objects" type="Spring.Context.Support.DefaultSectionHandler, Spring.Core" />
<section name="parsers" type="Spring.Context.Support.NamespaceParsersSectionHandler, Spring.Core" />
</sectionGroup>
</configSections>

<spring>
<parsers>
<parser type="Spring.Data.Config.DatabaseNamespaceParser, Spring.Data" />
</parsers>

<context>
<resource uri="assembly://Dataconnection/Dataconnection/Componet.xml"/>
<!--配置注入的容器-->
<resource uri="config://spring/objects" />
</context>

<objects xmlns="http://www.springframework.net" xmlns:r="http://www.springframework.net/remoting">
<!--這個是必須的-->
</objects>
</spring>
</configuration>

代码调用:

            IApplicationContext demo = ContextRegistry.GetContext();
var vidp = (Spring.Data.Core.AdoTemplate)demo.GetObject("adoTemplate");
int i = (int) vidp.Execute(new CommandDelegate(delegate(IDbCommand dbCommand)
{
dbCommand.CommandText =
"select count(*) from dbo.mfworkdetail(nolock) where workorderno='001400000005' ";
return (int)dbCommand.ExecuteScalar();
}));


以上执行正常没问题.....希望高人指点啊!