1 Replies - 5541 Views - Last Post: 26 December 2009 - 10:22 PM Rate Topic: -----

#1 TrueWave  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 20
  • Joined: 12-August 09

Can't Connect to Local Database?

Posted 26 December 2009 - 04:02 PM

Hi,

I'm learning SQL commands and such with C#, but I can't seem to get past simply connecting to the database.

I get this when I try to connect:

System.Data.SqlClient.SqlException: An attempt to attach an auto-named database for file C:\Users\Peppers\Documents\Visual Studio 2008\Projects\TestApp1\TestApp1\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.
   at System.Data.SqlClient.SqlInternalConnection.onerror(SqlException exception, Boolean breakConnection)
   at System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj)
   at System.Data.SqlClient.TdsParser.Run(Runbehavior runbehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj)
   at System.Data.SqlClient.SqlInternalConnectionTds.CompleteLogin(Boolean enlistOK)
   at System.Data.SqlClient.SqlInternalConnectionTds.AttemptOneLogin(ServerInfo serverInfo, String newPassword, Boolean ignoreSniOpenTimeout, Int64 timerExpire, SqlConnection owningObject)
   at System.Data.SqlClient.SqlInternalConnectionTds.LoginNoFailover(String host, String newPassword, Boolean redirectedUserInstance, SqlConnection owningObject, SqlConnectionString connectionOptions, Int64 timerStart)
   at System.Data.SqlClient.SqlInternalConnectionTds.OpenLoginEnlist(SqlConnection owningObject, SqlConnectionString connectionOptions, String newPassword, Boolean redirectedUserInstance)
   at System.Data.SqlClient.SqlInternalConnectionTds..ctor(DbConnectionPoolIdentity identity, SqlConnectionString connectionOptions, Object providerInfo, String newPassword, SqlConnection owningObject, Boolean redirectedUserInstance)
   at System.Data.SqlClient.SqlConnectionFactory.CreateConnection(DbConnectionOptions options, Object poolGroupProviderInfo, DbConnectionPool pool, DbConnection owningConnection)
   at System.Data.ProviderBase.DbConnectionFactory.CreatePooledConnection(DbConnection owningConnection, DbConnectionPool pool, DbConnectionOptions options)
   at System.Data.ProviderBase.DbConnectionPool.CreateObject(DbConnection owningObject)
   at System.Data.ProviderBase.DbConnectionPool.UserCreateRequest(DbConnection owningObject)
   at System.Data.ProviderBase.DbConnectionPool.GetConnection(DbConnection owningObject)
   at System.Data.ProviderBase.DbConnectionFactory.GetConnection(DbConnection owningConnection)
   at System.Data.ProviderBase.DbConnectionClosed.OpenConnection(DbConnection outerConnection, DbConnectionFactory connectionFactory)
   at System.Data.SqlClient.SqlConnection.Open()
   at com.day7.sql.SQL.Connect(String conStr) in C:\Users\Peppers\Documents\Visual Studio 2008\Projects\TestApp1\TestApp1\com.day7.sql\SQL.cs:line 21


Here's the code I use:

public static bool Connect(string conStr)
		{
			try
			{
				connection = new SqlConnection(conStr);
				connection.ConnectionString = conStr;
				connection.Open();
			}
			catch (Exception ex) {Error(ex.ToString()); return false; }
			return true;
		}


This is the ConnectionString I pass to Connect():
data source=.\SQLEXPRESS;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|aspnetdb.mdf;User Instance=true

Help?

Is This A Good Question/Topic? 0
  • +

Replies To: Can't Connect to Local Database?

#2 eclipsed4utoo  Icon User is offline

  • Not Your Ordinary Programmer
  • member icon

Reputation: 1525
  • View blog
  • Posts: 5,961
  • Joined: 21-March 08

Re: Can't Connect to Local Database?

Posted 26 December 2009 - 10:22 PM

You don't attach a sql express database. Remove that and it should work(assuming that you are actually using sql express)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1