我正在使用此代码通过网络连接到我的数据库:
// 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();
}
但是抛出了这个异常:底层提供程序在打开时失败。用户“sa”的登录失败。我在 Sql Server Managment Studio 中测试了用户名、密码、服务器名和数据库名,它正在工作。我怎样才能修复我的代码?