2016-03-27 13 views
0

私はビジュアルスタジオでデータベースを作成しようとしていますが、できません。私はエラーメッセージが表示されます: "ログインできませんでした"データベース1 "データベースを開くことができませんログインは失敗しましたユーザー '123'のログインに失敗しました" SQL Serverスタジオで手動で "database1"を作成し、私はデータベースの細かい部分に接続することができます。私はID 123とパスワードabcを持つユーザーをサーバースタジオに作成しました。すべてのサーバーの役割にチェックが入っています。私は、SQL Expressを使用していたコメントをもとに、2012年データベースをプログラムで作成できません

private void sQLToolStripMenuItem_Click(object sender, EventArgs e) 
    { 
     bool create_db = false; 
     bool create_table = false; 


     SqlConnection myConnection = new SqlConnection("Server = localhost; Database = database1; User Id = 123; Password = abc;"); 
     try 
     { 

      myConnection.Open(); 
      MessageBox.Show("We were able to connect to the database!", "Success!"); 
      create_table = true; 
     } 
     catch (Exception ex) 
     { 
      Console.WriteLine(ex.ToString()); 
      create_db = true; 
      MessageBox.Show("There is no database. We will create one.", "No Database Found."); 
     } 

     // For creating the database if not found 
     if (create_db == true) 
     { 
      String str; 
      SqlConnection myConn = new SqlConnection("Server = localhost; Database = database1; User Id = 123; Password = abc;"); 

      str = "CREATE DATABASE database1"; 

      SqlCommand myCommand = new SqlCommand(str, myConn); 
      try 
      { 
       myConn.Open(); 
       myCommand.ExecuteNonQuery(); 
       MessageBox.Show("DataBase is Created Successfully", "Creation", MessageBoxButtons.OK, MessageBoxIcon.Information); 
       create_db = false; 
      } 
      catch (System.Exception ex) 
      { 
       MessageBox.Show(ex.ToString(), "MyProgram", MessageBoxButtons.OK, MessageBoxIcon.Information); 
      } 
      finally 
      { 
       if (myConn.State == ConnectionState.Open) 
       { 
        myConn.Close(); 
       } 
      } 
     } 
+0

存在しないデータベースは指定できません。データベースを作成する必要がある場合は、「初期カタログ」や「データベース」の値を指定しないでください。データベースを正常に作成したら、データベースを指定できます。 –

+0

私はSQLを初めて使っているので、 "Database = database1;"接続文字列から?私は完全に理解していない "あなたが正常にデータベースを作成した後、私はそれを指定することができます。 – Vince

+0

@Vince様、最初のカタログとしてmasterデータベースを使用することをお勧めします。 – mostafa8026

答えて

1

:あなたがプログラムでデータベースを作成しようとしている場合は、このような状況では、接続を開く必要がありますので、あなたは、胎児のデータベースへの接続を使用することはできません masterデータベース、そのようなことに:

"Server = localhost; Database = master; User Id = 123; Password = abc;" 
0
 String str; 
     SqlConnection myConn = new SqlConnection("Server=localhost;User Id = 123; Password = abc;database=master"); 

     str = "CREATE DATABASE database1"; 

     SqlCommand myCommand = new SqlCommand(str, myConn); 
     try 
     { 
      myConn.Open(); 
      myCommand.ExecuteNonQuery(); 
      MessageBox.Show("DataBase is Created Successfully", "Creation", MessageBoxButtons.OK, MessageBoxIcon.Information); 

     } 
     catch (System.Exception ex) 
     { 
      MessageBox.Show(ex.ToString(), "MyProgram", MessageBoxButtons.OK, MessageBoxIcon.Information); 
     } 
     finally 
     { 
      if (myConn.State == ConnectionState.Open) 
      { 
       myConn.Close(); 
      } 
     } 

この上記のコードを試してみてください。私のために働いています。あなたはデータベースを作成する前にデータベース名を与えないでください。データベース接続を初期化するには、データベースマスターを使用します。

関連する問題