无法连接到SQL Server:“用户登录失败". [英] Can't connect to SQL Server: "Login failed for user "."
问题描述
我是.NET的新手,遇到了一堵砖墙.我正在用C#编写代码来访问Microsoft SQL Server2008.这是我的app.config文件中的代码
I'm new with .NET and have hit a brick wall. I'm writing code in C# to access a Microsoft SQL Server 2008. This is the code from my app.config file
<configuration>
<appSettings>
<add key="provider" value="System.Data.SqlClient" />
</appSettings>
<connectionStrings>
<add name ="AutoLotSqlProvider" connectionString =
"Data Source=.\SQLEXPRESS;AttachDbFilename=C:\Program Files\Microsoft SQL Server\MSSQL10_50.SQLEXPRESS\MSSQL\DATA\AutoLot.mdf"/>
<add name ="AutoLotOleDbProvider" connectionString =
"Provider=SQLOLEDB;Data Source=.\SQLEXPRESS;AttachDbFilename=C:\Program Files\Microsoft SQL Server\MSSQL10_50.SQLEXPRESS\MSSQL\DATA\AutoLot.mdf"/>
</connectionStrings>
</configuration>
当我调试C#程序时,我收到此错误消息:
When I debug the C# program I'm getting this error message:
System.Data.SqlClient.SqlException {用户登录失败." }
System.Data.SqlClient.SqlException { Login failed for user "." }
我在数据库中找不到用户名
I cannot find a user name in the database
这是我的程序代码:
public class Program
{
static void Main(string[] args)
{
// Get Connection string/provider from *.config.
Console.WriteLine("***** Fun with Data Provider Factories *****\n");
string dp = ConfigurationManager.AppSettings["provider"];
string cnStr = ConfigurationManager.ConnectionStrings["AutoLotSqlProvider"].ConnectionString;
// Get the factory provider.
DbProviderFactory df = DbProviderFactories.GetFactory(dp);
// Now make connection object.
using (DbConnection cn = df.CreateConnection())
{
Console.WriteLine("Your connection object is a: {0}", cn.GetType().Name);
cn.ConnectionString = cnStr;
cn.Open();
if (cn is SqlConnection)
{
// Print out which version of SQL Server is used.
Console.WriteLine(((SqlConnection)cn).ServerVersion);
}
// Make command object.
DbCommand cmd = df.CreateCommand();
Console.WriteLine("Your command object is a: {0}", cmd.GetType().Name);
cmd.Connection = cn;
cmd.CommandText = "Select * From Inventory";
// Print out data with data reader.
using (DbDataReader dr = cmd.ExecuteReader())
{
Console.WriteLine("Your data reader object is a: {0}", dr.GetType().Name);
Console.WriteLine("\n***** Current Inventory *****");
while (dr.Read())
Console.WriteLine("-> Car #{0} is a {1}.",
dr["CarID"], dr["Make"].ToString());
}
}
}
}
推荐答案
在您的连接字符串中,您尚未指定要Windows身份验证还是SQL身份验证.对于SQL身份验证,应该是(显然用您的用户名和密码替换x和y):
In your connection string, you haven't specified whether you want Windows Authentication or SQL Authentication. For SQL auth it should be (obviously replace x and y with your username and password):
<add name ="AutoLotSqlProvider" connectionString =
"Data Source=.\SQLEXPRESS;User ID=x;Password=y;AttachDbFilename=C:\...\AutoLot.mdf"/>
对于Windows身份验证,应为:
For Windows auth it should be:
<add name ="AutoLotSqlProvider" connectionString =
"Data Source=.\SQLEXPRESS;Integrated Security=SSPI;AttachDbFilename=C:\...\AutoLot.mdf"/>
这篇关于无法连接到SQL Server:“用户登录失败".的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!