根据 web.config 检查 sqlce 数据库是否存在
请参阅问题如何检查数据库是否存在? 我试图根据 web.config 连接字符串检测数据库是否存在,但运气不好。
我使用上面的最后一个答案作为我的试验。
试验一: web.config 中的连接字符串
<add name="sqlCEConnString" connectionString="Data Source=|DataDirectory|db.sdf;Initial Catalog=master" providerName="System.Data.SqlServerCE.4.0"/>
我收到错误:不支持关键字:'初始目录'。
试验 2:没有关键字
字符串 conString = ConfigurationManager.ConnectionStrings["sqlCEConnString "].ConnectionString;
using(SqlCeConnection cnn = new SqlCeConnection(conString))
{
cnn.Open();
using (SqlCeCommand com = new SqlCeCommand("select count(*) from sys.databases where name = 'db.sdf'" , cnn))
{
int j=com.ExecuteNonQuery();
Response.Write("Result:" + j);
}
}
这次我得到了以下错误: 指定的表不存在。 [@@sys.databases]
我做错了什么?
Refer to the question How to check for the existence of a DB? I was trying to detect the existence of a DB as per in web.config connection string, but out of luck.
I was using the last answer of the above as my trial.
Trial 1:
connection string in the web.config
<add name="sqlCEConnString" connectionString="Data Source=|DataDirectory|db.sdf;Initial Catalog=master" providerName="System.Data.SqlServerCE.4.0"/>
I got the error: Keyword not supported: 'initial catalog'.
Trial 2: without the keyword
string conString = ConfigurationManager.ConnectionStrings["sqlCEConnString"].ConnectionString;
using(SqlCeConnection cnn = new SqlCeConnection(conString))
{
cnn.Open();
using (SqlCeCommand com = new SqlCeCommand("select count(*) from sys.databases where name = 'db.sdf'" , cnn))
{
int j=com.ExecuteNonQuery();
Response.Write("Result:" + j);
}
}
This time I got the following error:
The specified table does not exist. [ @@sys.databases ]
What I am doing wrong?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
最后我找到了根据连接字符串检测数据库是否存在的方法。在“SqlCeEngine”类下有一个名为“Verify”的方法,可以通过该方法进行检测。
Finally I found the way to detect whether the DB exists as per connection string. Under the "SqlCeEngine" class there is a method named "Verify" by which it is possible to detect.
看一下这里的源代码,它检测 ASP.NET 应用程序中是否存在 SQL CE 数据库文件,并在需要时使用表创建它: http://sqlcemembership.codeplex.com
Have a look at the source code here, it detects if a SQL CE database file is present in an ASP.NET app, and creates it with tables if required: http://sqlcemembership.codeplex.com
我认为您需要指定 sqlce 数据库所在的连接。
试试这个链接。
Connectionstring.com / Sql Compact
问候!
I Think you need to specify the connections where sqlce database is located.
try this link.
Connectionstring.com / Sql Compact
Regards!