主题:如何用自定义方法写增删改查
如何用自定义方法写增删改查,特别三层结构的开发中,经常用到,很多不懂,如下面的代码,我怎么才可以知道定义这个方法,需要在方法括号中定义这些参数,如数据连接 、SQL语句...?
/// <summary>
/// 执行ExecuteNonQuery
/// </summary>
/// <param name="connString">数据库连接</param>
/// <param name="cmdType">Sql语句类型</param>
/// <param name="cmdText">Sql语句</param>
/// <param name="cmdParms">Parm数组</param>
/// <returns>返回影响行数</returns>
public static int ExecuteNonQuery(string connString , CommandType cmdType, string cmdText ,params SqlParameter[] cmdParms)
{
SqlCommand cmd=new SqlCommand();
using (SqlConnection conn=new SqlConnection(connString))
{
conn.Open();
cmd.Connection=conn;
cmd.CommandText=cmdText;
if(cmdParms!=null)
{
foreach(SqlParameter parm in cmdParms)
cmd.Parameters.Add(parm);
}
int val=cmd.ExecuteNonQuery();
cmd.Parameters.Clear();
conn.Close();
return val;
}
}
/// <summary>
/// 执行ExecuteNonQuery
/// </summary>
/// <param name="connString">数据库连接</param>
/// <param name="cmdType">Sql语句类型</param>
/// <param name="cmdText">Sql语句</param>
/// <param name="cmdParms">Parm数组</param>
/// <returns>返回影响行数</returns>
public static int ExecuteNonQuery(string connString , CommandType cmdType, string cmdText ,params SqlParameter[] cmdParms)
{
SqlCommand cmd=new SqlCommand();
using (SqlConnection conn=new SqlConnection(connString))
{
conn.Open();
cmd.Connection=conn;
cmd.CommandText=cmdText;
if(cmdParms!=null)
{
foreach(SqlParameter parm in cmdParms)
cmd.Parameters.Add(parm);
}
int val=cmd.ExecuteNonQuery();
cmd.Parameters.Clear();
conn.Close();
return val;
}
}