私はビジュアルスタジオでデータベースを作成しようとしていますが、できません。私はエラーメッセージが表示されます: "ログインできませんでした"データベース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();
}
}
}
存在しないデータベースは指定できません。データベースを作成する必要がある場合は、「初期カタログ」や「データベース」の値を指定しないでください。データベースを正常に作成したら、データベースを指定できます。 –
私はSQLを初めて使っているので、 "Database = database1;"接続文字列から?私は完全に理解していない "あなたが正常にデータベースを作成した後、私はそれを指定することができます。 – Vince
@Vince様、最初のカタログとしてmasterデータベースを使用することをお勧めします。 – mostafa8026