[英]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.