无法连接到SQL Server:“用户登录失败”。“

时间:2011-08-27 21:29:22

标签: c# .net sql-server-2008 authentication

我是.NET的新手并且遇到了困难。我正在用C#编写代码来访问Microsoft SQL Server 2008.这是我的app.config文件中的代码

<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#程序时,我收到此错误消息:

  

System.Data.SqlClient.SqlException {登录用户失败“。” }

我在数据库中找不到用户名

这是我的程序代码:

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());
            }
        }
    }
}

3 个答案:

答案 0 :(得分:4)

在连接字符串中,您尚未指定是要进行Windows身份验证还是SQL身份验证。对于SQL身份验证,它应该是(显然用您的用户名和密码替换x和y):

<add name ="AutoLotSqlProvider" connectionString = 
 "Data Source=.\SQLEXPRESS;User ID=x;Password=y;AttachDbFilename=C:\...\AutoLot.mdf"/>

对于Windows身份验证,它应该是:

<add name ="AutoLotSqlProvider"  connectionString =
 "Data Source=.\SQLEXPRESS;Integrated Security=SSPI;AttachDbFilename=C:\...\AutoLot.mdf"/>

答案 1 :(得分:1)

您需要指定用户名和密码才能登录Sql数据库。

要获取连接字符串列表,请选中ConnectionStrings

sql 2008的OLEDB连接字符串为Provider=SQLNCLI10;Server=myServerAddress;Database=myDataBase;Uid=myUsername; Pwd=myPassword;

答案 2 :(得分:1)

您的连接字符串缺少身份验证方案。您需要在创建连接时传递用户名/密码或使用集成安全性。