title: C# 2010 Express + SQL Server 2008 Express - Connection "Login failed"
body: <p>I am actually developing a Windows Forms Application with Visual C# Express 2010    which would use (read/write) data from a SQL Server 2008 Express DB</p>
  
  <p>I have created my DB with SQL Server Management Studio (2008 Express), 
  I understand the instance is named <code>ATLELAG786576\SQLEXPRESS</code>
  My DB is called 'TEST'</p>
  
  <p>Looking at my DB 'TEST' Properties in SQL Server Management Studio (2008 Express):
  Under Files, I am (<code>ATLE\bneveux</code>) the owner of the DB</p>
  
  <p>Looking under Security, Logins, Mylogin (ATLE\bneveux)</p>
  
  <ul>
  <li>My default DB is 'TEST'</li>
  <li>Server roles are 'public' + 'sysadmin'</li>
  <li>User Mapping DB 'TEST' User 'dbo' Default Schema 'dbo'</li>
  </ul>
  
  <p>In my C# application</p>
  
  <p><strong>app.config:</strong></p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8" ?&gt; &lt;configuration&gt;
      &lt;configSections&gt;
      &lt;/configSections&gt;
      &lt;connectionStrings&gt;
          &lt;add name="connectionStringTestDb"
              connectionString="Data Source=ATLELAG786576\SQLEXPRESS;Initial Catalog=D:\Microsoft SQL Server\MSSQL10.SQLEXPRESS\MSSQL\DATA\TEST.mdf;Integrated Security=True;Connect Timeout=30;User Instance=False"
              providerName="System.Data.SqlClient" /&gt;
      &lt;/connectionStrings&gt; &lt;/configuration&gt;
  </code></pre>
  
  <p><strong>dbConnection.cs:</strong></p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.Linq;
  using System.Text;
  using System.Data;
  using System.Data.SqlClient;
  using System.Configuration;
  
  namespace SQLServerConnectionDemo
  {
      class dbConnection
      {
          public static SqlConnection newCon;
          public static string connectionStringTestDb = ConfigurationManager.ConnectionStrings["connectionStringTestDb"].ConnectionString;
  
          public static SqlConnection GetConnection()
          {
              newCon = new SqlConnection(connectionStringTestDb);
              return newCon;
          }
      }
  }
  </code></pre>
  
  <p><strong>dbAccess.cs:</strong></p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.Linq;
  using System.Text;
  using System.Data;
  using System.Data.SqlClient;
  
  namespace SQLServerConnectionDemo
  {
      class dbAccess
      {
          SqlConnection conn;
          public dbAccess()
          {
              conn = dbConnection.GetConnection();
          }
  
          //Method insert new in tblEmployees
          public void addEmployee(string Id, string Name, string Email)
          {
              if (conn.State.ToString() == "Closed")
              {
                  conn.Open();
              }
              SqlCommand newCmd = conn.CreateCommand();
              newCmd.Connection = conn;
              newCmd.CommandType = CommandType.Text;
              newCmd.CommandText = "INSERT INTO tblEmployees VALUES ('"+ Id +"','"+ Name +"','"+ Email +"')";
              newCmd.ExecuteNonQuery();
          }
  
      }
  }
  </code></pre>
  
  <p><strong>in a form formEmployeeAdd.cs:</strong></p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.ComponentModel;
  using System.Data;
  using System.Drawing;
  using System.Linq;
  using System.Text;
  using System.Windows.Forms;
  
  namespace SQLServerConnectionDemo
  {
      public partial class formEmployeeAdd : Form
      {
  
          dbAccess access = new dbAccess();
  
          public formEmployeeAdd()
          {
              InitializeComponent();
          }
  
          private void btnInsert_Click(object sender, EventArgs e)
          {
              access.addEmployee(txtId.Text, txtName.Text, txtEmail.Text);
              MessageBox.Show("Data successfully added");
          }
      }
  }
  </code></pre>
  
  <p>And here the error message I always get when trying to run this process:</p>
  
  <blockquote>
    <p>System.Data.SqlClient.SqlException (0x80131904): Cannot open database "D:\Microsoft SQL Server\MSSQL10.SQLEXPRESS\MSSQL\DATA\TEST.mdf" requested by the login. The login failed.
    Login failed for user 'ATLE\bneveux'.</p>
  </blockquote>
  
  <p>Note that I have never really been able to add my Data Source in Visual C# 2010 Express so I could manage the DB from VS, I always get the following error message:</p>
  
  <blockquote>
    <p>Unable to open the physical file "D:\Microsoft SQL Server\MSSQL10.SQLEXPRESS\MSSQL\DATA\TEST.mdf". Operating system error 32: "32(Le processus ne peut pas accéder au fichier car ce fichier est utilisé par un autre processus.)".
    An attempt to attach an auto-named database for file D:\Microsoft SQL Server\MSSQL10.SQLEXPRESS\MSSQL\DATA\TEST.mdf failed. A database with the same name exists, or specified file cannot be opened, or it is located on UNC share.</p>
  </blockquote>