使用参数化连接时是否需要关闭连接
Do i need to close the connection when using parametrised connection
我有一段代码如下所示,我在其中打开连接并在其上执行命令。我是否必须关闭此连接或编写这段代码的最佳方式是什么?
SqlConnection con = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["ConnectionString"].ToString());
if (con.State == ConnectionState.Closed)
{
con.Open();
}
string sql = "INSERT INTO RegisterUser(Name,LastName,email,Nationality,Country) VALUES (@param1,@param2,@param3,@param4,@param5)";
SqlCommand cmd = new SqlCommand(sql, con);
cmd.Parameters.Add("@param1", SqlDbType.NVarChar, 200).Value = txtName.Text;
cmd.Parameters.Add("@param2", SqlDbType.NVarChar, 100).Value = txtLastName.Text;
cmd.Parameters.Add("@param3", SqlDbType.NVarChar, 50).Value = txtEmail.Text;
cmd.Parameters.Add("@param4", SqlDbType.NVarChar, 50).Value = ddCountry.SelectedItem.Value.ToString();
cmd.Parameters.Add("@param5", SqlDbType.NVarChar, 50).Value = txtCountryCode.Text;
cmd.CommandType = CommandType.Text;
cmd.ExecuteNonQuery();
如果需要关闭连接,上面的代码不会释放内存吗?
您应该关闭它,否则在执行终结器之前,底层资源将一直被使用。
最好的选择是实际处理连接(在数据库连接对象的特定情况下等于关闭它):
using (SqlConnection con = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["ConnectionString"].ToString()))
{
con.Open();
string sql = "INSERT INTO RegisterUser(Name,LastName,email,Nationality,Country) VALUES (@param1,@param2,@param3,@param4,@param5)";
SqlCommand cmd = new SqlCommand(sql, con);
cmd.Parameters.Add("@param1", SqlDbType.NVarChar, 200).Value = txtName.Text;
cmd.Parameters.Add("@param2", SqlDbType.NVarChar, 100).Value = txtLastName.Text;
cmd.Parameters.Add("@param3", SqlDbType.NVarChar, 50).Value = txtEmail.Text;
cmd.Parameters.Add("@param4", SqlDbType.NVarChar, 50).Value = ddCountry.SelectedItem.Value.ToString();
cmd.Parameters.Add("@param5", SqlDbType.NVarChar, 50).Value = txtCountryCode.Text;
cmd.CommandType = CommandType.Text;
cmd.ExecuteNonQuery();
}
是的,您需要始终关闭连接。你也不需要第一个 if.
using(SqlConnection con = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["ConnectionString"].ToString()))
{
con.Open();
string sql = "INSERT INTO RegisterUser(Name,LastName,email,Nationality,Country) VALUES (@param1,@param2,@param3,@param4,@param5)";
SqlCommand cmd = new SqlCommand(sql, con);
cmd.Parameters.Add("@param1", SqlDbType.NVarChar, 200).Value = txtName.Text;
cmd.Parameters.Add("@param2", SqlDbType.NVarChar, 100).Value = txtLastName.Text;
cmd.Parameters.Add("@param3", SqlDbType.NVarChar, 50).Value = txtEmail.Text;
cmd.Parameters.Add("@param4", SqlDbType.NVarChar, 50).Value = ddCountry.SelectedItem.Value.ToString();
cmd.Parameters.Add("@param5", SqlDbType.NVarChar, 50).Value = txtCountryCode.Text;
cmd.CommandType = CommandType.Text;
cmd.ExecuteNonQuery();
}
所以using
会自动为您关闭连接。即使您的代码抛出异常,也会发生这种情况。 using
代表try/catch/finally块。通过这种方式,如果发生错误,您可以保证连接是 return 到 Connection Pool
。
try
{
Sqlconnection conn = new SqlConnection("your conn string");
}
catch(Exception ex)
{
throw;
}
finally
{
conn.Close();
}
您可以使用 'using'
using(SqlConnection(System.Configuration.ConfigurationManager
.ConnectionStrings["ConnectionString"].ToString())){
if (con.State == ConnectionState.Closed)
{
con.Open();
}
string sql = "INSERT INTO RegisterUser(Name,LastName,email,Nationality,Country) VALUES (@param1,@param2,@param3,@param4,@param5)";
SqlCommand cmd = new SqlCommand(sql, con);
cmd.Parameters.Add("@param1", SqlDbType.NVarChar, 200).Value = txtName.Text;
cmd.Parameters.Add("@param2", SqlDbType.NVarChar, 100).Value = txtLastName.Text;
cmd.Parameters.Add("@param3", SqlDbType.NVarChar, 50).Value = txtEmail.Text;
cmd.Parameters.Add("@param4", SqlDbType.NVarChar, 50).Value = ddCountry.SelectedItem.Value.ToString();
cmd.Parameters.Add("@param5", SqlDbType.NVarChar, 50).Value = txtCountryCode.Text;
cmd.CommandType = CommandType.Text;
cmd.ExecuteNonQuery();
}
我有一段代码如下所示,我在其中打开连接并在其上执行命令。我是否必须关闭此连接或编写这段代码的最佳方式是什么?
SqlConnection con = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["ConnectionString"].ToString());
if (con.State == ConnectionState.Closed)
{
con.Open();
}
string sql = "INSERT INTO RegisterUser(Name,LastName,email,Nationality,Country) VALUES (@param1,@param2,@param3,@param4,@param5)";
SqlCommand cmd = new SqlCommand(sql, con);
cmd.Parameters.Add("@param1", SqlDbType.NVarChar, 200).Value = txtName.Text;
cmd.Parameters.Add("@param2", SqlDbType.NVarChar, 100).Value = txtLastName.Text;
cmd.Parameters.Add("@param3", SqlDbType.NVarChar, 50).Value = txtEmail.Text;
cmd.Parameters.Add("@param4", SqlDbType.NVarChar, 50).Value = ddCountry.SelectedItem.Value.ToString();
cmd.Parameters.Add("@param5", SqlDbType.NVarChar, 50).Value = txtCountryCode.Text;
cmd.CommandType = CommandType.Text;
cmd.ExecuteNonQuery();
如果需要关闭连接,上面的代码不会释放内存吗?
您应该关闭它,否则在执行终结器之前,底层资源将一直被使用。
最好的选择是实际处理连接(在数据库连接对象的特定情况下等于关闭它):
using (SqlConnection con = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["ConnectionString"].ToString()))
{
con.Open();
string sql = "INSERT INTO RegisterUser(Name,LastName,email,Nationality,Country) VALUES (@param1,@param2,@param3,@param4,@param5)";
SqlCommand cmd = new SqlCommand(sql, con);
cmd.Parameters.Add("@param1", SqlDbType.NVarChar, 200).Value = txtName.Text;
cmd.Parameters.Add("@param2", SqlDbType.NVarChar, 100).Value = txtLastName.Text;
cmd.Parameters.Add("@param3", SqlDbType.NVarChar, 50).Value = txtEmail.Text;
cmd.Parameters.Add("@param4", SqlDbType.NVarChar, 50).Value = ddCountry.SelectedItem.Value.ToString();
cmd.Parameters.Add("@param5", SqlDbType.NVarChar, 50).Value = txtCountryCode.Text;
cmd.CommandType = CommandType.Text;
cmd.ExecuteNonQuery();
}
是的,您需要始终关闭连接。你也不需要第一个 if.
using(SqlConnection con = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["ConnectionString"].ToString()))
{
con.Open();
string sql = "INSERT INTO RegisterUser(Name,LastName,email,Nationality,Country) VALUES (@param1,@param2,@param3,@param4,@param5)";
SqlCommand cmd = new SqlCommand(sql, con);
cmd.Parameters.Add("@param1", SqlDbType.NVarChar, 200).Value = txtName.Text;
cmd.Parameters.Add("@param2", SqlDbType.NVarChar, 100).Value = txtLastName.Text;
cmd.Parameters.Add("@param3", SqlDbType.NVarChar, 50).Value = txtEmail.Text;
cmd.Parameters.Add("@param4", SqlDbType.NVarChar, 50).Value = ddCountry.SelectedItem.Value.ToString();
cmd.Parameters.Add("@param5", SqlDbType.NVarChar, 50).Value = txtCountryCode.Text;
cmd.CommandType = CommandType.Text;
cmd.ExecuteNonQuery();
}
所以using
会自动为您关闭连接。即使您的代码抛出异常,也会发生这种情况。 using
代表try/catch/finally块。通过这种方式,如果发生错误,您可以保证连接是 return 到 Connection Pool
。
try
{
Sqlconnection conn = new SqlConnection("your conn string");
}
catch(Exception ex)
{
throw;
}
finally
{
conn.Close();
}
您可以使用 'using'
using(SqlConnection(System.Configuration.ConfigurationManager
.ConnectionStrings["ConnectionString"].ToString())){
if (con.State == ConnectionState.Closed)
{
con.Open();
}
string sql = "INSERT INTO RegisterUser(Name,LastName,email,Nationality,Country) VALUES (@param1,@param2,@param3,@param4,@param5)";
SqlCommand cmd = new SqlCommand(sql, con);
cmd.Parameters.Add("@param1", SqlDbType.NVarChar, 200).Value = txtName.Text;
cmd.Parameters.Add("@param2", SqlDbType.NVarChar, 100).Value = txtLastName.Text;
cmd.Parameters.Add("@param3", SqlDbType.NVarChar, 50).Value = txtEmail.Text;
cmd.Parameters.Add("@param4", SqlDbType.NVarChar, 50).Value = ddCountry.SelectedItem.Value.ToString();
cmd.Parameters.Add("@param5", SqlDbType.NVarChar, 50).Value = txtCountryCode.Text;
cmd.CommandType = CommandType.Text;
cmd.ExecuteNonQuery();
}