Issue when connecting ASP.NET project to SQL Server Compact 4.0

22 March 2012

在Review一个ASP.NET项目时,尝试将原来的SQL Server Express数据库转换到SQL Server Compact 4.0

首先修改了web.config文件中的连接字符串:

<add name="LocalSqlServer"
         connectionString="Data Source=C:\Documents\mydata.sdf;Password=mypwd;Persist Security Info=True"
         providerName="System.Data.SqlServerCe.4.0"/>

可是页面总是返回错误信息:"A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible."

在经过N次无用的搜索和尝试后,终于在StackOverflow找到了解决方法。原来需要在connect string和页面的SqlDataSource中同时指明providerName="System.Data.SqlServerCe.4.0"

例如,页面中调用数据操作的代码为:

<asp:SqlDataSource providerName="System.Data.SqlServerCe.4.0" ID="SqlDataSourceCustomer" runat="server" ConnectionString="<%$ ConnectionStrings:LocalSqlServer %>"
                    SelectCommand="SELECT '-Select Fleet-' AS NAME, '0' AS Id UNION SELECT Name, Id FROM [Customer] ORDER BY Id ASC" />


下一篇: Eclipse IDE环境查看Android SDK源码 →

blog comments powered by Disqus