Reputation: 3502
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?
Upvotes: 2
Views: 2160
Reputation: 1650
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());
Upvotes: 2