62,046
社区成员
发帖
与我相关
我的任务
分享
public static SqlDataReader getrs(String sql)
{
SqlConnection con = greatcon();//获取连接。详细就不写了
con.Open();
SqlCommand exec = new SqlCommand(sql, con);
SqlDataReader rs = exec.ExecuteReader(CommandBehavior.CloseConnection);//听说这样可以直接关闭CON
return rs;
}
String sql="select * from table1"
SqlDataReader rs = getrs(sql) //调用上面的方法
rs.Read();
XXXXXXXXX
rs.Close();
rs.Dispo…… //这样调用SQL。Con都被关闭了吗?
public static int ExecuteNonQuery(SqlConnection connection, CommandType commandType, string commandText, params SqlParameter[] commandParameters)
{
if( connection == null ) throw new ArgumentNullException( "connection" );
// Create a command and prepare it for execution
SqlCommand cmd = new SqlCommand();
bool mustCloseConnection = false;
PrepareCommand(cmd, connection, (SqlTransaction)null, commandType, commandText, commandParameters, out mustCloseConnection );
// Finally, execute the command
int retval = cmd.ExecuteNonQuery();
// Detach the SqlParameters from the command object, so they can be used again
cmd.Parameters.Clear();
if( mustCloseConnection )
connection.Close();
return retval;
}
using (SqlConnection con = new SqlConnection(constr))
{
using (SqlCommand com = new SqlCommand("update TabMailList set classid = @classid where mailid = @mailid",con))
{
com.Parameters.Add("@classid",classid);
com.Parameters.Add("@mailid",mailid);
con.Open();
com.ExecuteNonQuery();
}
}