我用 C# 编写我的数据库命令如下
using (SqlConnection con = new SqlConnection(Config.ConnectionString))
{
using (SqlCommand cmd = new SqlCommand("INSERT INTO ProjectDetails(pro_name,pro_location,pro_briefdesc,pro_desc,pro_res,pro_contact,pro_add,pro_email,pro_phone) VALUES (@name,@loc,@brief,@desc,@res,@cont,@add,@email,@ph)", con))
{
cmd.Parameters.AddWithValue("@name",pro_name);
cmd.Parameters.AddWithValue("@loc",pro_location );
cmd.Parameters.AddWithValue("@brief",pro_briefdesc);
cmd.Parameters.AddWithValue("@desc", pro_desc);
cmd.Parameters.AddWithValue("@res",pro_res);
cmd.Parameters.AddWithValue("@cont",pro_contact);
cmd.Parameters.AddWithValue("@add",pro_add);
cmd.Parameters.AddWithValue("@email",pro_email);
cmd.Parameters.AddWithValue("@ph",pro_phone );
con.Open();
int modified = cmd.ExecuteNonQuery();
if (con.State == System.Data.ConnectionState.Open) con.Close();
return modified;
}
}
我怎样才能写我的连接打开和关闭通常 - 即最少的代码重复(重复)。