简体   繁体   中英

An attempt to attach an auto-named database for file \bin\Debug\aspnetdb.mdf failed

I am developing a Class Library in C# and in this Class Library I am trying to access database through ADO.NET code, But I'm getting this error. I don't know what is the reason behind it. So please tell me how can I solve it.

System.Data.SqlClient.SqlException: An attempt to attach an auto-named database for file C:\\Users\\vivek.nuna\\Documents\\VisualStudio2005\\Projects\\SubsystemSyncService\\TestClient\\bin\\Debug\\aspnetdb.mdf failed. A database with the same name exists, or specified file cannot be opened, or it is located on UNC share.

This is the connection string I am using.

<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>

Note: I can't see a file aspnetdb.mdf in the Debug folder.

This is the code, how I am using ado.net code.

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;
        }
    }

I have added reference of this class Library in a C# form.

C# form is calling this method of class library.

If you want a standalone database for your application, you should have a look at

SQL Server Compact : http://www.microsoft.com/sqlserver/2008/en/us/compact.aspx

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

For connectionstrings see 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";

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM