jerry li

博客园 首页 联系 订阅 管理
public class UtilDao : PageBase
{
    protected static string connectionString = WebConfigurationManager.ConnectionStrings["CawAdmin"].ConnectionString;
    public delegate void CommondDelegate(String[] pamerStr,SqlCommand cmd);

    public static DataRow getDemoRow(String sql,String[] pamerStr,CommondDelegate commondDelegate)
    {
        DataRow row = null;
        SqlConnection conn = new SqlConnection(connectionString);
        SqlCommand cmd = new SqlCommand(sql, conn);
        commondDelegate(pamerStr,cmd);
        SqlDataAdapter adapter = new SqlDataAdapter(cmd);
        DataSet ds = new DataSet();
        try
        {
            conn.Open();
            adapter.Fill(ds);
            row = ds.Tables[0].Rows[0];
        }
        catch (System.Exception ex)
        {
            throw ex;
        }
        finally
        {
            conn.Close();
        }
        return row;
    }

    public static DataTable getDemoTable(String sql, String[] pamerStr, CommondDelegate commondDelegate)
    {
        DataTable table = null;
        SqlConnection conn = new SqlConnection(connectionString);
        SqlCommand cmd = new SqlCommand(sql, conn);
        commondDelegate(pamerStr, cmd);
        SqlDataAdapter adapter = new SqlDataAdapter(cmd);
        DataSet ds = new DataSet();
        try
        {
            conn.Open();
            adapter.Fill(ds);
            table = ds.Tables[0];
        }
        catch (System.Exception ex)
        {
            throw ex;
        }
        finally
        {
            conn.Close();
        }
        return table;
    }

    public static DataTable getDemoTable(String sql)
    {
        DataTable table = null;
        SqlConnection conn = new SqlConnection(connectionString);
        SqlCommand cmd = new SqlCommand(sql, conn);
        SqlDataAdapter adapter = new SqlDataAdapter(cmd);
        DataSet ds = new DataSet();
        try
        {
            conn.Open();
            adapter.Fill(ds);
            table = ds.Tables[0];
        }
        catch (System.Exception ex)
        {
            throw ex;
        }
        finally
        {
            conn.Close();
        }
        return table;
    }

    public static void execQuery(String sql,String[] pamerStr, CommondDelegate commondDelegate)
    {
        SqlConnection conn = new SqlConnection(connectionString);
        SqlCommand cmd = null;
        try
        {
            conn.Open();
            cmd = new SqlCommand(sql, conn);
            commondDelegate(pamerStr, cmd);
            cmd.ExecuteNonQuery();
        }
        catch (System.Exception ex)
        {
            throw ex;
        }
        finally
        {
            conn.Close();
        }
    }

    public static void execQuery(SqlConnection conn, SqlTransaction sqlTrans, String sql, String[] pamer, CommondDelegate commondDelegate)
    {
        SqlCommand cmd = null;
        try
        {
            cmd = new SqlCommand(sql, conn);
            cmd.Transaction = sqlTrans;
            commondDelegate(pamer, cmd);
            cmd.ExecuteNonQuery();
        }
        catch (System.Exception ex)
        {
            throw ex;
        }
    }

    public static void execQuery(String sql)
    {
        SqlConnection conn = new SqlConnection(connectionString);
        SqlCommand cmd = new SqlCommand(sql, conn);
        try
        {
            conn.Open();
            cmd.ExecuteNonQuery();
        }
        catch (System.Exception ex)
        {
            throw ex;
        }
        finally
        {
            conn.Close();
        }
    }
}
posted on 2009-12-11 10:18  jerry li  阅读(206)  评论(0)    收藏  举报