【问题标题】:An attempt to attach an auto-named database for file \bin\Debug\aspnetdb.mdf failed尝试为文件 \bin\Debug\aspnetdb.mdf 附加自动命名数据库失败
【发布时间】:2013-12-02 10:28:34
【问题描述】:

我正在用 C# 开发一个类库,在这个类库中我试图通过 ADO.NET 代码访问数据库,但是我收到了这个错误。我不知道这背后的原因是什么。所以请告诉我我该如何解决它。

System.Data.SqlClient.SqlException:尝试为文件 C:\Users\vivek.nuna\Documents\VisualStudio2005\Projects\SubsystemSyncService\TestClient\bin\Debug\aspnetdb.mdf 附加自动命名数据库失败。存在同名数据库,或指定文件无法打开,或位于 UNC 共享上。

这是我正在使用的连接字符串。

<connectionStrings>
    <add name="RegistrationConnString" 
         connectionString="Server=192.168.101.145\SQLEXPRESS;Database=***_HubDB;User Id=sa;Password={C8273EFD-LB2F-4E65-8702-14B61PI08A9}" 
         providerName="System.Data.SqlClient" />
</connectionStrings>

注意:我在 Debug 文件夹中看不到文件 aspnetdb.mdf

这是代码,我如何使用 ado.net 代码。

private DataSet GetAddressFieldsAccordingtoAddressId(string strAddressId)
{
        try
        {                
            strConnString = ConfigurationManager.ConnectionStrings["RegistrationConnString"].ToString();
            SqlConnection connection = new SqlConnection(strConnString);
            SqlCommand command = new SqlCommand();

            command.CommandType = System.Data.CommandType.StoredProcedure;
            command.CommandText = "[dbo].[PL_spPLUIGetAddressFieldsAccordingtoAddressId]";
            command.Parameters.Add("@lAddressID", System.Data.SqlDbType.Int).Value = Convert.ToInt32(strAddressId);
            command.Connection = connection;

            DataSet dsPwd = new DataSet();               
            SqlDataAdapter dAdapter = new SqlDataAdapter(command);
            dAdapter.Fill(dsPwd);

            command.Dispose();
            dAdapter.Dispose();

            return dsPwd;
        }
        catch (Exception ex)
        {
            return null;
        }
    }

我以 C# 形式添加了对此类库的引用。

C#窗体调用类库的这个方法。

【问题讨论】:

  • 试试这个one

标签: c# asp.net sql-server-2008 ado.net connection-string


【解决方案1】:

如果您想要一个独立的数据库用于您的应用程序,您应该看看

SQL Server 精简版:http://www.microsoft.com/sqlserver/2008/en/us/compact.aspx

SQLite:http://sqlite.phxsoftware.com/

有关连接字符串,请参阅http://www.connectionstrings.com/access-2003/

SqlCeConnection sqlConnection1 = new SqlCeConnection();
sqlConnection1.ConnectionString = "Data Source = C:\\Users\\Administrator\\My Documents\\BMS_Data.sdf;Persist Security Info=False";

【讨论】:

  • 他已经在使用 .mdf = SQL Server 数据库文件 - 不知道你为什么开始谈论 Access....(那将是一个 .mdb 文件 - 不是 .mdf !)
  • 朋友们,我对这个世界很陌生,请解释一下。我不明白你们在讨论什么
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-09-15
  • 2012-02-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多