存储过程的封装
只是做一个例子,别的可以仿照下面的例子改改就好了。
代码
public class SqlHelp
{
SqlConnection con = new SqlConnection();
public SqlHelp(string server)//服务器
{
con.ConnectionString = server;
}
/// <summary>
/// Open Database
/// </summary>
private void OpenSqlHelp()
{
try
{
SqlConnection sqlcon = new SqlConnection();
sqlcon.ConnectionString = con.ConnectionString;
if (sqlcon.State != ConnectionState.Open)
{
this.con.Open();
}
}
catch (Exception e)
{
throw e;
}
}
/// <summary>
/// Close Database
/// </summary>
private void CloseSqlHelp()
{
if (con != null)
{
if (con.State == ConnectionState.Open)
{
con.Close();
}
}
}
// <summary>
/// 存储过程的封装
/// </summary>
/// <param name="TableName">Sql表名</param>
/// <param name="prams">参数</param>
/// <returns>返回INT类型,影响的行数</returns>
public int IntStoredProcedure(string TableName, SqlParameter[] prams)
{
SqlCommand com = new SqlCommand();
com.Connection = con;
com.CommandText = TableName;
com.CommandType = CommandType.StoredProcedure;
foreach (SqlParameter pram in prams)
{
com.Parameters.Add(pram);
}
try
{
OpenSqlHelp();
return Convert.ToInt32(com.ExecuteScalar());
}
catch (Exception ex)
{
throw ex;
}
finally
{
CloseSqlHelp();
}
}
}
}