Unable to connect to SQL Server: "Login failed for user."

I am new to .NET and hit a brick wall. I am writing C # code to access 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>

When I debug a C # program, I get this error message:

System.Data.SqlClient.SqlException {Login failed for user "." }

I can not find the username in the database

This is my program code:

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());
            }
        }
    }
}
+2
source share
3 answers

In your connection string, you did not indicate whether you want Windows authentication or SQL authentication. For SQL auth, this 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"/>

For windows auth, this should be:

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

Sql.

ConnectionStrings

OLEDB SQL Server 2008 Provider=SQLNCLI10;Server=myServerAddress;Database=myDataBase;Uid=myUsername; Pwd=myPassword;

+1

Your connection string is missing an authentication scheme. When creating a connection, you must either enter a username / password or use the built-in protection.

+1
source

All Articles