プログラムで SQL Server テーブルを作成しようとしています。これがコードです。
using (SqlConnection con = new SqlConnection(conStr))
{
try
{
//
// Open the SqlConnection.
//
con.Open();
//
// The following code uses an SqlCommand based on the SqlConnection.
//
using (SqlCommand command = new SqlCommand("CREATE TABLE Customer(First_Name char(50),Last_Name char(50),Address char(50),City char(50),Country char(25),Birth_Date datetime);", con))
command.ExecuteNonQuery();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
このアプリケーションを 2 回目に実行すると、例外が発生します。
「データベースには「Customer」という名前のオブジェクトが既に存在します」
しかし、データベースをチェックすると、そのようなテーブルは表示されません。
これが私の接続文字列です。
<connectionStrings>
<add name ="AutoRepairSqlProvider" connectionString=
"Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\AutoRepairDatabase.mdf;
Integrated Security=True;User Instance=True"/>
</connectionStrings>
選択クエリを実行しているとき。既存のテーブルから結果を取得しているので、接続文字列は問題ないと思います。問題が表示されることを願っています:/