2

I'm using this code for connect to my database over the network :

// Specify the provider name, server and database.
            string providerName = "System.Data.SqlClient";
            string serverName = "VENUS-PC";
            string databaseName = "Cheque";

            // Initialize the connection string builder for the
            // underlying provider.
            SqlConnectionStringBuilder sqlBuilder =
                new SqlConnectionStringBuilder();

            // Set the properties for the data source.
            sqlBuilder.DataSource = serverName;
            sqlBuilder.InitialCatalog = databaseName;
            //sqlBuilder.IntegratedSecurity = true;
            sqlBuilder.UserID = "sa";
            sqlBuilder.Password = "123";
            sqlBuilder.MultipleActiveResultSets = true;
            // Build the SqlConnection connection string.
            string providerString = sqlBuilder.ToString();

            // Initialize the EntityConnectionStringBuilder.
            EntityConnectionStringBuilder entityBuilder =
                new EntityConnectionStringBuilder();

            //Set the provider name.
            entityBuilder.Provider = providerName;

            // Set the provider-specific connection string.
            entityBuilder.ProviderConnectionString = providerString;

            // Set the Metadata location.
            entityBuilder.Metadata = @"res://*/Cheque.csdl|
                            res://*/Cheque.ssdl|
                            res://*/Cheque.msl";
            //Console.WriteLine(entityBuilder.ToString());
            System.Windows.Forms.MessageBox.Show(entityBuilder.ToString());

            using (EntityConnection conn =
                new EntityConnection(entityBuilder.ToString()))
            {
                conn.Open();
                //Console.WriteLine("Just testing the connection.");
                System.Windows.Forms.MessageBox.Show("Connection is Ok");
                conn.Close();
            }

But this exception Thrown : The underlying provider failed on open.Login Failed for user 'sa'. And I test the user name ,password ,server name and database name in Sql Server Managment Studio and it's working. How can i fix my code?

1
  • Look at the answers on this question. Commented Sep 28, 2013 at 19:17

1 Answer 1

2
public static string GetConStrSQL()
{
    string connectionString = new System.Data.EntityClient.EntityConnectionStringBuilder
    {
        Metadata = "res://*",
        Provider = "System.Data.SqlClient",
        ProviderConnectionString = new System.Data.SqlClient.SqlConnectionStringBuilder
        {
            InitialCatalog = "Name Of The Database",
            DataSource = @"ServerNameHere\SQLEXPRESS",
            IntegratedSecurity = false,
            UserID = "sa",               
            Password = "your_password_here",  
        }.ConnectionString
    }.ConnectionString;

    return connectionString;
}

and in the creation of the context :

YouEFContext ctx = new YouEFContext(GetConStrSQL());
Sign up to request clarification or add additional context in comments.

1 Comment

it's always only 1 damn line: Metadata = "res://*", missing :D thank you mate! have an upvote

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.