title: Code to create & connect to a SQL Server database: Whats wrong with it?
body: <p>I am new to C# &amp; I am trying to programatically create &amp; open a SQL Server database. </p>
  
  <p>I have a ASP.NET webapp I am creating &amp; on page load it should pull some data from the database (if the db doesn't exist, it should be created &amp; populated with default data). </p>
  
  <p>PS: does C#'s <code>System.Data.SqlClient</code> use MySQL or SQLite or something else?</p>
  
  <p>Right now I am unsure if my code correctly creates a SQL Server database &amp; if I connect to it correctly.</p>
  
  <p>Can you tell me if my code is correct &amp; how I could improve it?</p>
  
  <p><strong>UPDATE:</strong> Error is </p>
  
  <blockquote>
    <p>"A network-related or instance-specific error occurred while
    establishing a connection to SQL Server. The server was not found or
    was not accessible. Verify that the instance name is correct and that
    SQL Server is configured to allow remote connections. (provider: Named
    Pipes Provider, error: 40 - Could not open a connection to SQL
    Server)"}"</p>
  </blockquote>
  
  <p>I have indicated where in the code below the error occurs.</p>
  
  <p>Creating a SQL Server database:</p>
  
  <pre><code>    // When I run this function no file seems to be created in my project directory?
      // Although there is a ASPNETDB sql database file in my App_Data folder so this maybe it
      public static string DEF_DB_NAME = "mydb.db"; // is this the correct extension?
      private bool populateDbDefData()
      {
          bool res = false;
          SqlConnection myConn = new SqlConnection("Server=localhost;Integrated security=SSPI;database=master");
          string str = "CREATE DATABASE "+DEF_DB_NAME+" ON PRIMARY " +
              "(NAME = " + DEF_DB_NAME + "_Data, " +
              "FILENAME = " + DEF_DB_NAME + ".mdf', " +
              "SIZE = 2MB, MAXSIZE = 10MB, FILEGROWTH = 10%) " +
              "LOG ON (NAME = " + DEF_DB_NAME + "_Log, " +
              "FILENAME = " + DEF_DB_NAME + "Log.ldf', " +
              "SIZE = 1MB, " +
              "MAXSIZE = 5MB, " +
              "FILEGROWTH = 10%)";
  
  
          SqlCommand myCommand = new SqlCommand(str, myConn);
          try
          {
              myConn.Open(); // ERROR OCCURS HERE
              myCommand.ExecuteNonQuery();
              insertDefData(myConn);
          }
          catch (System.Exception ex)
          {
              res = false;
          }
          finally
          {
              if (myConn.State == ConnectionState.Open)
                   myConn.Close();
              res = true;
          }
  
          return res;
      }
  </code></pre>
  
  <p>Here's my connection to the SQL Server database code: I am pretty sure it fails to connect -  if I try to use the variable conn, it says the connection is not open. Which could mean that I either failed to connect or failed to even create the db in the 1st place:</p>
  
  <pre><code>    private bool connect()
      {
          bool res = false;
          try
          {
              conn = new SqlConnection("user id=username;" +
                                       "password=password;" +
                                       "Server=localhost;" +
                                       "Trusted_Connection=yes;" +
                                       "database="+DEF_DB_NAME+"; " +
                                       "connection timeout=30");
              conn.Open();
              return true;
          }
          catch (Exception e)
          {
          }
  
          return false;
      }
  </code></pre>