簡體   English   中英

使用SQL Server的應用程序中的SqlException

[英]SqlException in app using SQL Server

嘿,我剛剛開始學習使用后端作為sql在C#中制作應用程序。 我放置了一些斷點,以查看執行cntCeilInn.Open();之后該控件永不返回cntCeilInn.Open(); 不久,在輸出窗口顯示“ System.Data.SqlClient.SqlException ”發生在System.Data.dll

Google告訴我它沒有超時或我無法理解的事情。 請一些幫助。

string strServerName="EXPRESSION";

using (SqlConnection cntCeilInn = new SqlConnection("Data Source=" + strServerName + ";" + "Integrated Security=YES"))
{
    SqlCommand cmdCeilInn= new SqlCommand("If exists ("+ "Select name "+ "from sys.database "+ "Where name=N'CeilInn1')"+ "DROP database CeilInn1;"+ "go"+ "create database CeilInn1;", cntCeilInn);
    cntCeilInn.Open();
    cmdCeilInn.ExecuteNonQuery();
}

作為connectionString參數,您應該使用:

Data Source=yourServerName;Initial Catalog=yourDatabaseName;Integrated Security=True

確保通過適當的數據修改youServerNameyourDatabaseName

請注意,您忘記在connectionString( Initial Catalog )中設置數據庫名稱,而Integrated Security的值為True

sql查詢中幾乎沒有錯誤。

  1. 它是sys.databases而不是sys.database

  2. GoCreate語法之間沒有空格

  3. 您需要將sql語句放在單獨的行中

  4. 更不用說像其他人所說的那樣檢查您的connection string

試試看

SqlCommand cmdCeilInn = new SqlCommand("If exists (" + "Select name " + "from sys.databases " + "Where name=N'Sample') DROP database Sample;" 
                      +Environment .NewLine+ "go " + Environment .NewLine 
                     +" create database Sample;", cntCeilInn);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM