增删改,已经写好了,但是还不知道如何写查询语句,返回DataTable
请各位帮下忙,小弟上班不久,还没用过存储语句        private static void PrepareCommand(SqlCommand cmd, SqlConnection conn, SqlTransaction trans, CommandType cmdType, string cmdText, SqlParameter[] commandParameters)
        {            //Open the connection if required
            if (conn.State != ConnectionState.Open)
                conn.Open();            //Set up the command
            cmd.Connection = conn;
            cmd.CommandText = cmdText;
            cmd.CommandType = cmdType;            //Bind it to the transaction if it exists
            if (trans != null)
                cmd.Transaction = trans;            // Bind the parameters passed in
            if (commandParameters != null)
            {
                foreach (SqlParameter parm in commandParameters)
                    cmd.Parameters.Add(parm);
            }
        }
        public static string ExecuteSql(CommandType commandType, String strSql, params SqlParameter[] commandParameters)
        {
            SqlConnection cn = Connection;
            try
            {
                SqlCommand cmd = new SqlCommand();
                PrepareCommand(cmd, cn, (SqlTransaction)null, commandType, strSql, commandParameters);
                cmd.ExecuteNonQuery();
                cmd.Parameters.Clear();
                return "1";
            }
            catch (Exception e)
            {
                return e.Message;
            }
            finally
            {
                Connection.Close();
            }
        }

解决方案 »

  1.   

    根本不应该在 dbhelper里面直接写查询。
      

  2.   

    这个我清楚,我是想问下,DBleper这个层中如何返回DataTable。
      

  3.   

    这两个都不行。在你的  dbhelper 里面找找有没有类似
    DataSet 函数名(string sql)
    或者
    DataTable 函数名(string sql)
    之类的函数。或者你可以下载一个微软标准的 dbhelper。
      

  4.   

    http://www.google.com.hk/#hl=zh-CN&newwindow=1&safe=strict&q=sqlhelper.cs+&oq=sqlhelper.cs+&aq=f&aqi=g1&aql=&gs_sm=e&gs_upl=21145l21145l7l21389l1l1l0l0l0l0l161l161l0.1l1l0&bav=on.2,or.r_gc.r_pw.,cf.osb&fp=7f5d5b12d409e946&biw=1280&bih=929http://blog.csdn.net/bnmjstu/article/details/4355932
      

  5.   

    http://www.cnblogs.com/cresuccess/archive/2008/12/10/1351675.html看看吧,說的很詳細,思路也很清晰 ,每層做什麽
      

  6.   

     #region//绑定热销产品的类别
            /// <summary>
            /// 绑定热销产品的类别HotProductKind
            /// </summary>
            /// <param name="">不需要参数</param>
            /// <returns>返回热销产品类别</returns>
            public static DataTable gethotproductkind()
            {
                SqlCommand cmd = new SqlCommand("HotProductKind",Con);
                cmd.CommandType = CommandType.StoredProcedure;
                DataSet ds = new DataSet();
                SqlDataAdapter da = new SqlDataAdapter(cmd);
                da.Fill(ds);
                return ds.Tables[0];
            }
            #endregion
      

  7.   

    HotProductKind这个就是你的查询存储过程例如
    create proc HotProductKind
    as
    begin
    select * from Test;
    end