我想知道开设公司的最佳方法是什么SqlConnection
with Sql Server 2008R2 Express Edition
数据库。这个版本的Sql有RAM使用和CPU使用的限制,所以我们必须采取最好的办法来打开一个SqlConnection
.
现在我正在检查每个方法的开始和结束时的连接。这是一个例子。
private void CheckValidId(string Id)
{
CheckConnectionStatus();
try
{
sqlConnection.Open();
sqlCommand = new SqlCommand("select * from ValidId where id=@id", sqlConnection);
sqlCommand.Parameters.AddWithValue("@id", Id);
sqlDataReader = sqlCommand.ExecuteReader();
While (sqlDataReader.Read())
{
string Test = sqlDataReader["Id"].toString();
MessageBox.Show("Value of Id : " , Test);
}
}
catch (Exception exp)
{
MessageBox.Show(exp.Message.ToString(), "Exception in CheckValidId");
}
finally
{
CheckConnectionStatus();
}
}
Here is CheckConnectionStatus
Method
private void CheckConnectionStatus()
{
if (sqlConnection.State == ConnectionState.Open)
{
sqlConnection.Close();
}
}
执行此操作的最佳方法是什么。
Thanks
只需使用using
因为一旦完成它就会处理连接。
using(SqlConnection conn = new SqlConnection("Connection string")){
//do sql stuff
conn.Open();
//etc etc
conn.Close();
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)