0
我有一個問題,直到現在我總是添加並建立一個連接到我的視覺工作室使用Add-> new existing item等等,但現在我的老師告訴我只用代碼連接到數據庫和我一樣波紋管Visual Studio 2017中的SQL數據庫
string ds = "Data Source=localhost";
string db = "Initial Catalog=";
string ins = "Integrated Security=True";
string nume = "Companie2";
SqlConnection cursCon = new SqlConnection(@ds + ";" + db + ";" +
ins);
SqlCommand cursCom1 = cursCon.CreateCommand();
String command = "CREATE DATABASE [" + nume + "]";
try
{
cursCon.Open();
cursCom1.CommandText = command;
cursCom1.ExecuteNonQuery();
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
但是,當我去到服務器資源管理器我的數據庫是不存在的,也讓我不知道是什麼問題我沒有得到任何錯誤訊息,我應該怎麼做修復它?謝謝
是的,但是我對服務器資源管理器所說的只是一句話,但仍然無法修復,我仔細觀察,發現有一些錯誤,例如「建立連接時出現網絡相關或實例特定錯誤到SQL服務器「,我搜查了一下,仍然不知道該怎麼辦... – user3720015
你有沒有在代碼中放置斷點並逐步調試?我可以看到連接字符串的問題:'@ds +「;」 + db +「;」 + ins'將評估爲'Data Source = localhost; Initial Catalog =; Integrated Security = True'。 「初始目錄」是空白的。嘗試將其設置爲數據庫(因爲您嘗試執行創建數據庫的命令,所以我建議'master')。 –
'ds' <>'@ ds'沒有變量'@ ds'。 –