我有个公有数据库查询方法:
执行查询操作
  public static Object ExecuteObject(string sql, params SqlParameter[] param)
        {
            SqlDataReader reader = null;
            Object obj = null;
            SqlCommand cmd = new SqlCommand();
            using (SqlConnection con = new SqlConnection(strConn))
            {
                cmd.Connection = con;
                cmd.CommandText = sql;
                cmd.CommandType = CommandType.StoredProcedure;                if (param.Length > 0)
                {
                    foreach (SqlParameter p in param)
                    {
                        cmd.Parameters.Add(p);
                    }
                }
                try
                {
                    if (con.State == ConnectionState.Closed)
                        con.Open();
                    reader = cmd.ExecuteReader();
                    while (reader.Read())
                    {
                        obj = reader[0];
                    }
                }
                catch (Exception ex)
                {
                    string msg = ex.Message;
                }
                finally
                { }
            }
            return obj;
        }    }
}

我想把处理这个button事件得代码,换成调用公有的方法。
  protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            using (SqlConnection conn = new SqlConnection(strconn))
            {
                using (SqlCommand cmd = new SqlCommand("get_newsId", conn))
                {
                    try
                    { 
                        //获取页面传递的id
                        int id = Convert.ToInt32(Request.QueryString["id"]);
                        cmd.CommandType = CommandType.StoredProcedure;
                        cmd.Parameters.AddWithValue("@NEWID", id);
                        conn.Open();
                        SqlDataReader dr = cmd.ExecuteReader();
                        while (dr.Read())
                        {
                            txttle.Text = dr["NEW_NAME"].ToString();
                            txtCont.Text = dr["NEW_CONTENT"].ToString();
                            txtTime.Text = dr["NEW_TIME"].ToString();                        }
                        conn.Close();                    }
                    catch (Exception ex)
                    {
                        Response.Write(ex.ToString());
                    }
                                  }
            }
         
        }
    }
}
我知道怎么写,请各位提示一下,本人是菜鸟

解决方案 »

  1.   

    button的事件在哪?在实现什么功能哈?
    protected void Page_Load(object sender, EventArgs e) 
        { 
            if (!IsPostBack) 
            { 
                using (SqlConnection conn = new SqlConnection(strconn)) 
                { 
                    using (SqlCommand cmd = new SqlCommand("get_newsId", conn)) 
                    { 
                        try 
                        { 
                            //获取页面传递的id 
                            int id = Convert.ToInt32(Request.QueryString["id"]); 
                            cmd.CommandType = CommandType.StoredProcedure; 
                            cmd.Parameters.AddWithValue("@NEWID", id); 
                            conn.Open(); 
                            SqlDataReader dr = cmd.ExecuteReader(); 
                            while (dr.Read()) 
                            { 
                                txttle.Text = dr["NEW_NAME"].ToString(); 
                                txtCont.Text = dr["NEW_CONTENT"].ToString(); 
                                txtTime.Text = dr["NEW_TIME"].ToString();                         } 
                            conn.Close();                     } 
                        catch (Exception ex) 
                        { 
                            Response.Write(ex.ToString()); 
                        } 
                                      } 
                } 
            
            } 
        } 
    这个是执行存储过程的,你数据库里有存储过程
      

  2.   

    正解:
    protected void Page_Load(object sender, EventArgs e) 
        { 
            if (!IsPostBack) 
            { 
                 string sql = "get_newsId";
                 try 
                 { 
                            //获取页面传递的id 
                            int id = Convert.ToInt32(Request.QueryString["id"]); 
                            SqlParameter param= new SqlParameter("@NEWID", id); 
                            object obj = 你的类名.ExecuteObject(sql,param);
                            //在这儿使用obj(公共方法返回的结果)              } 
                  catch (Exception ex) 
                  { 
                            Response.Write(ex.ToString()); 
                  } 
        }