SqlDependency.Start 尝试附加文件的自动命名数据库失败

发布于 2024-12-06 02:44:20 字数 1909 浏览 0 评论 0原文

我在我的 App_Data 文件夹中获得了 NORTHWND.mdf 和 NORTHWND.LOG 的副本

我的连接字符串:

   <add name="northwind_connection" connectionString="Data Source=.\SQLEXPRESS;AttachDbFileName=|DataDirectory|NORTHWND.mdf;Integrated Security=True;User Instance=True" providerName="System.Data.SqlClient" />

当我尝试打开和关闭连接时,一切正常。

  string connStr = WebConfigurationManager.ConnectionStrings["northwind_connection"].ToString();            
  SqlConnection conn = new SqlConnection(connStr);            
  SqlCommand command = new SqlCommand("Select * From Products");
  command.Connection = conn;                
  conn.Open();
  SqlDataReader reader = command.ExecuteReader();            
  GridView1.DataSource = reader;
  GridView1.DataBind();
  conn.Close();

现在除了这段代码之外,我想将 SqlCacheDependency 添加到页面 当我放置代码: 显示在 msdn 中时,

     SqlDependency.Start(connStr);

我收到以下错误:

尝试附加文件 C:\Program Files (x86)\Common Files\Microsoft Shared\DevServer\10.0\NORTHWND.mdf 的自动命名数据库 失败的。存在同名数据库,或无法打开指定文件,或位于 UNC 共享上。

任何想法为什么会发生这种情况,我需要配置什么才能使 SqlCacheDependency 工作。

提前致谢 埃兰。

另外我想补充一点,如果我将连接字符串更改为特定的连接字符串,

  <add name="northwind_connection" connectionString="Data Source=.\SQLEXPRESS;AttachDbFilename=C:\Program Files\Microsoft SQL Server\MSSQL10_50.SQLEXPRESS\MSSQL\DATA\NORTHWND.MDF; Integrated Security=True" providerName="System.Data.SqlClient" />

一切都会正常工作,但这似乎是错误的,因为我不希望用户将连接字符串更改为他们的路径,这就是为什么我想把它在App_Data中 或者在列表中给出 .\SQLEXPRESS 的相对路径 这也不起作用:

  <add name="myConnection"  connectionString="Data Source=.\SQLEXPRESS;Initial Catalog=NORTHWND;Integrated Security=True;" providerName="System.Data.SqlClient"/>

请阐明这个问题,必须有一些配置才能实现这一点。 提前致谢。 埃兰。

iv'e got copy of NORTHWND.mdf along with NORTHWND.LOG in my App_Data folder

MY CONNECTION STRING :

   <add name="northwind_connection" connectionString="Data Source=.\SQLEXPRESS;AttachDbFileName=|DataDirectory|NORTHWND.mdf;Integrated Security=True;User Instance=True" providerName="System.Data.SqlClient" />

when i attempt to open and close the connection everything works out fine.

  string connStr = WebConfigurationManager.ConnectionStrings["northwind_connection"].ToString();            
  SqlConnection conn = new SqlConnection(connStr);            
  SqlCommand command = new SqlCommand("Select * From Products");
  command.Connection = conn;                
  conn.Open();
  SqlDataReader reader = command.ExecuteReader();            
  GridView1.DataSource = reader;
  GridView1.DataBind();
  conn.Close();

now beside this code i want to add SqlCacheDependency to the page
when i place the code : Shown in msdn

     SqlDependency.Start(connStr);

I GET THE FOLLOWING ERROR :

An attempt to attach an auto-named database for file C:\Program Files (x86)\Common Files\Microsoft Shared\DevServer\10.0\NORTHWND.mdf failed. A database with the same name exists, or specified file cannot be opened, or it is located on UNC share.

any ideas why this happens , what do i need to configure for the SqlCacheDependency to work.

thanks in advance
eran.

in addition i would like to add that if i change my connection string to a specific one

  <add name="northwind_connection" connectionString="Data Source=.\SQLEXPRESS;AttachDbFilename=C:\Program Files\Microsoft SQL Server\MSSQL10_50.SQLEXPRESS\MSSQL\DATA\NORTHWND.MDF; Integrated Security=True" providerName="System.Data.SqlClient" />

everything works as it should but that seems wrong since i don't expect users to change the connection string to their path , that's why i would like to put it in App_Data
or at list give a relative path to .\SQLEXPRESS
which also doesn't work :

  <add name="myConnection"  connectionString="Data Source=.\SQLEXPRESS;Initial Catalog=NORTHWND;Integrated Security=True;" providerName="System.Data.SqlClient"/>

please shed some light on this issue there must be some configuration that makes this possible .
thanks in advance.
eran.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

花桑 2024-12-13 02:44:20

我认为您不能将 SqlCacheDependency自动附加 (SQLEXPRESS) 类型的连接字符串一起使用。

您需要在 Management studio 中附加数据库,并将连接字符串更改为如下所示:

server=(local);database=Northwind;Integrated Security=SSPI;

然后您需要执行 ALTER DATABASE NORTHWIND SET ENABLE_BROKER

如果您需要为用户提供这种设置,那么您可以编写一个 SQL 脚本来为他们​​做这件事。

I don't think you can use SqlCacheDependency with an auto-attach (SQLEXPRESS) type connection string.

You need to attach the database in Management studio, and change your connection string to look like:

server=(local);database=Northwind;Integrated Security=SSPI;

Then you need to execute ALTER DATABASE NORTHWIND SET ENABLE_BROKER

If you need to provide this kind of setup for users then you can write a SQL Script that will do it for them.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文