tags:

views:

91

answers:

3

I am in my first steps using db in an application so my goal is to build simple db with one table from scratch in order to improve and learn

I have opened new db using the visual studio : Tolls -> connect to database and announced him to create new data base

the next step was to populate a table with 4 columns (the all 4 are nchar(10))

and then (with no actual data inside )i tried this code:

      try
        {
            // step 1: create a SqlConnection object to connect to the
            // SQL Server my server connection string proprties  database
    // i think the bug should be here !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
            SqlConnection mySqlConnection = new SqlConnection("Data Source=./SQLEXPRESS;AttachDbFilename=C:/Users/STERN/Documents/myDB.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True");//"server=localhost;database=Northwind;uid=sa;pwd=sa");
            // step 2: create a SqlCommand object
            SqlCommand mySqlCommand = mySqlConnection.CreateCommand();
            // step 3: set the CommandText property of the SqlCommand object to
            // a SQL SELECT statement that retrieves a row from the Customers table
            mySqlCommand.CommandText =
            "SELECT type " +
            "FROM Table1 " +
            "WHERE type = ‘ALFKI’";
            // step 4: open the database connection using the
            // Open() method of the SqlConnection object
            mySqlConnection.Open();

/* some code .... */

catch (SqlException e)
        {
            Console.WriteLine("A SqlException was thrown");
            Console.WriteLine("Number = " + e.Number);
            Console.WriteLine("Message = " + e.Message);
            Console.WriteLine("StackTrace:\n" + e.StackTrace);
        }
        string s = Console.ReadLine();
    }

where the exception i get is throwen by mySqlConnection.Open(); this is the error massage i get :

A SqlException was thrown Number = 3 Message = A network-related or instance-specific error occurred while establishi ng a connection to SQL Server. The server was not found or was not accessible. V erify that the instance name is correct and that SQL Server is configured to all ow remote connections. (provider: Named Pipes Provider, error: 40 - Could not op en a connection to SQL Server) StackTrace: at System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception , Boolean breakConnection) at System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning() at System.Data.SqlClient.TdsParser.Connect(ServerInfo serverInfo, SqlInternal ConnectionTds connHandler, Boolean ignoreSniOpenTimeout, Int64 timerExpire, Bool ean encrypt, Boolean trustServerCert, Boolean integratedSecurity) at System.Data.SqlClient.SqlInternalConnectionTds.AttemptOneLogin(ServerInfo serverInfo, String newPassword, Boolean ignoreSniOpenTimeout, TimeoutTimer timeo ut, SqlConnection owningObject) at System.Data.SqlClient.SqlInternalConnectionTds.LoginNoFailover(ServerInfo serverInfo, String newPassword, Boolean redirectedUserInstance, SqlConnection ow ningObject, SqlConnectionString connectionOptions, TimeoutTimer timeout) at System.Data.SqlClient.SqlInternalConnectionTds.OpenLoginEnlist(SqlConnecti on owningObject, TimeoutTimer timeout, SqlConnectionString connectionOptions, St ring newPassword, Boolean redirectedUserInstance) at System.Data.SqlClient.SqlInternalConnectionTds..ctor(DbConnectionPoolIdent ity identity, SqlConnectionString connectionOptions, Object providerInfo, String newPassword, SqlConnection owningObject, Boolean redirectedUserInstance) at System.Data.SqlClient.SqlConnectionFactory.CreateConnection(DbConnectionOp tions options, Object poolGroupProviderInfo, DbConnectionPool pool, DbConnection owningConnection) at System.Data.ProviderBase.DbConnectionFactory.CreatePooledConnection(DbConn ection owningConnection, DbConnectionPool pool, DbConnectionOptions options) at System.Data.ProviderBase.DbConnectionPool.CreateObject(DbConnection owning Object) at System.Data.ProviderBase.DbConnectionPool.UserCreateRequest(DbConnection o wningObject) at System.Data.ProviderBase.DbConnectionPool.GetConnection(DbConnection ownin gObject) at System.Data.ProviderBase.DbConnectionFactory.GetConnection(DbConnection ow ningConnection) at System.Data.ProviderBase.DbConnectionClosed.OpenConnection(DbConnection ou terConnection, DbConnectionFactory connectionFactory) at System.Data.SqlClient.SqlConnection.Open() at ConsoleApplication1.Program.Main(String[] args) in C:\Users\STERN\AppData\ Local\Temporary Projects\ConsoleApplication1\Program.cs:line 29

if someone knows about pdf , article ,something that would help me it would b very nice of him to write it down here... can someone please help me....

+1  A: 

It looks like you are unable to connect to your local instance of SQL Server Express.

Even though you are specifying a local database file, that file is going to be attached to the local instance of SQL Server Express once it connects. All data access is then done through the attached database through SQL Server Express (not directly from the file).

Make sure that is up, running, and you can connect to it using SQL Management Studio Express.

If your local instance of SQL server is up and responding or you want different behavior from your connection string, I would suggest checking out ConnectionStrings.com:

SQL Server 2008 Connection Strings

Justin Niessner
my lack of knowledge playes a prt here how i know it works ? and should this SqlConnection mySqlConnection = new SqlConnection("Data Source=./SQLEXPRESS;AttachDbFilename=C:/Users/STERN/Documents/myDB.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True");//"server=localhost;database=Northwind;uid=sa;pwd=sa"); be changed ?
yoav.str
@yoav.str - If you have a local file that you need to attach to SQL Server Express on connecting, then yes. It's hard to answer because I don't know how you set up your database or how you want things to behave.
Justin Niessner
thank you .I just want to build simple db with one table from scratch in order to improve and learn if you have an article or somewhere where i can read it all from .
yoav.str
A: 

Try connecting to your database with the same connection string in SQL Server Management Studio. Does it connect? If not (and I doubt it will), you have the wrong connection string.

Edit:

Additionally, make sure the SQL Server and SQL Agent services are running.

Andy_Vulhop
A: 

I would have to say the application has trouble connecting to your SQL server, if the SQL connection string is correct, check with your SQL Surface Area and Configurations that piped names are allowed and remote connections allowed.

-- edit --

Microsoft documentation on how to configure SQL Express for remote connections http://support.microsoft.com/kb/914277

Jason Jong
how? is there an article how to do it ?
yoav.str
@ yoav.str - see the documentation at http://support.microsoft.com/kb/914277
Jason Jong