title: Can't connect to SQL Server: "Login failed for user "."
body: <p>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</p>
  
  <pre><code>&lt;configuration&gt;
    &lt;appSettings&gt;
      &lt;add key="provider" value="System.Data.SqlClient" /&gt;
    &lt;/appSettings&gt;
    &lt;connectionStrings&gt;
        &lt;add name ="AutoLotSqlProvider"  connectionString =
             "Data Source=.\SQLEXPRESS;AttachDbFilename=C:\Program Files\Microsoft SQL  Server\MSSQL10_50.SQLEXPRESS\MSSQL\DATA\AutoLot.mdf"/&gt;   
       &lt;add name ="AutoLotOleDbProvider"  connectionString =
       "Provider=SQLOLEDB;Data Source=.\SQLEXPRESS;AttachDbFilename=C:\Program Files\Microsoft SQL Server\MSSQL10_50.SQLEXPRESS\MSSQL\DATA\AutoLot.mdf"/&gt;
       &lt;/connectionStrings&gt;
  &lt;/configuration&gt;
  </code></pre>
  
  <p>When I debug the C# program I'm getting this error message:  </p>
  
  <blockquote>
    <p>System.Data.SqlClient.SqlException { Login failed for user "." }</p>
  </blockquote>
  
  <p>I cannot find a user name in the database</p>
  
  <p>This is my program code:</p>
  
  <pre><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("-&gt; Car #{0} is a {1}.",
                        dr["CarID"], dr["Make"].ToString());
              }
          }
      }
  }
  </code></pre>