我有一個C#Windows窗體應用程序。我推一個按鈕,它應該創建一個表並插入一個值(int)。 我將初始數據庫創建爲服務數據庫(添加新項目>數據>服務數據庫)。無法在SQL Server中創建兩次相同的表
該按鈕的代碼是(輸出遵循其下方):
private void button1_Click(object sender, EventArgs e)
{
SqlConnection thisConnection = new SqlConnection(Properties.Settings.Default.Database1ConnectionString);
SqlCommand nonqueryCommand = thisConnection.CreateCommand();
try
{
thisConnection.Open();
nonqueryCommand.CommandText = "CREATE TABLE MyTable1 (intColumn int)";
Console.WriteLine(nonqueryCommand.CommandText);
Console.WriteLine("Number of Rows Affected is: {0}", nonqueryCommand.ExecuteNonQuery());
nonqueryCommand.CommandText = "INSERT INTO MyTable1 VALUES (99)";
Console.WriteLine(nonqueryCommand.CommandText);
Console.WriteLine("Number of Rows Affected is: {0}",
nonqueryCommand.ExecuteNonQuery());
}
catch (SqlException ex)
{
Console.WriteLine(ex.ToString());
}
finally
{
thisConnection.Close(); // close connection
Console.WriteLine("Connection Closed.");
}
}
OUTPUT: CREATE TABLE MYTABLE1(intColumn INT) 受影響的行號是:-1 INSERT INTO MYTABLE1 VALUES (99) 受影響的行數是:1 連接已關閉。
服務器資源管理器上沒有任何顯示即使我關閉並重新連接,也沒有附加表。
如果我按下按鈕,使其發出相同的,我再次得到:在服務器資源管理器
System.Data.SqlClient.SqlException (0x80131904): There is already an object named 'MyTable1' in the database.
,但仍然一無所獲。
有什麼原因令您感到愉快? – 2011-02-23 01:30:22