【发布时间】:2012-03-28 22:31:41
【问题描述】:
在我的 Windows 应用程序中,我尝试使用以下代码连接到 SQL Server 2008:
SqlConnection connection = new SqlConnection(Properties.Settings.Default.KargarBandarConnectionString);
SqlCommand command = new SqlCommand("Select IsAdmin from Users where UserName=@UserName And Password=@Password", connection);
SqlDataReader dataReader = null;
command.Parameters.AddWithValue("@UserName", UserNameTextBox.Text.Trim());
command.Parameters.AddWithValue("@Password", PasswordTextBox.Text);
try
{
connection.Open();
dataReader = command.ExecuteReader();
if (dataReader.HasRows)
{
while (dataReader.Read())
{
IsAdmin = dataReader.GetBoolean(0);
}
this.DialogResult = DialogResult.OK;
}
else
{
FMessageBox.ShowWarning("error");
UserNameTextBox.Focus();
}
}
catch (Exception ex)
{
if (progressForm != null)
progressForm.Close();
FMessageBox.ShowError(ex.Message);
}
finally
{
if (dataReader != null)
{
dataReader.Close();
dataReader.Dispose();
}
if (connection != null)
{
connection.Close();
connection.Dispose();
}
}
一切正常,但有时我会收到以下错误:
超时。在获得一个之前经过的超时时间 来自池的连接...
如何解决?
【问题讨论】:
-
增加连接超时时间。
-
您确定连接到 SQL Server 的所有其他位置在完成后断开/关闭/释放连接吗?
-
我的网络很小,从服务器接收到的信息很少。我真的不知道为什么会出现这个错误!
-
“和密码=@Password”。在某个地方,每次有人将明文密码作为 SQL 参数传递时,一只小猫就会死去:(
-
尝试使用
using语句来处理您的IDisposable对象。这将包括(至少)connection、command和dataReader。
标签: c# winforms sql-server-2008 timeout