[英]Exception when attempting to connect to database
我有一個非常簡單的數據庫,其中包含一個名為score的表,其中包含名為id,count和GameDate的列,當我從Web服務(visual studio)測試它時,它會向我返回false而不是true。 所以它沒有運行我的查詢,我似乎無法找到它的任何錯誤。
有人知道嗎?
碼
public bool SaveScore(int Score)
{
string connString = "Data Source=.SQLEXPRESS;Initial Catalog=Balloon_Math;Integrated Security=True";
CultureInfo culture = new CultureInfo("en-US");
string myQuery = "INSERT INTO score(count, GameDate) VALUES (" + Score + ",'" + DateTime.Now.ToString("d", culture) + "')";
SqlConnection myConnection = new SqlConnection(connString);
SqlCommand myCommand = new SqlCommand(myQuery, myConnection);
try
{
myConnection.Open();
myCommand.ExecuteNonQuery();
return true;
}
catch
{
return false;
}
finally
{
myConnection.Close();
}
}
在黑暗中刺,但我猜這個位錯了Data Source=.SQLEXPRESS
應該是類似Data Source=.\\SQLEXPRESS
東西
將你的捕獲更改為:
catch (Exception ex)
{
return false;
}
然后在return false;
放置一個斷點return false;
並看看ex
的價值是什么
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.