日期:2014-05-17  浏览次数:20543 次

使用存储过程,怎么使用SqlDataSource获取数据?
使用存储过程,怎么使用SqlDataSource获取数据?

------解决方案--------------------
存储过程

就不要用SqlDataSource了吧?

C# code
using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.Collections;

namespace M_Common.SqlHelper
{

    /******************************

    ******************************/
    public class SqlHelper
    {
        private static SqlConnection cn = null;
        private static SqlCommand cmd = null;

        public SqlHelper()
        {

        }

        /// <summary>
        /// 判断连接状态
        /// </summary>
        /// <returns>返回连接状态</returns>
        private static SqlConnection GetConn()
        {
            string ConnStr = M_Common.Helper.ConfigHelper.GetconnMsSql.ToString();
            cn = new SqlConnection(ConnStr);
            if (cn.State != ConnectionState.Open)
            {
                cn.Open();
            }
            return cn;
        }


        /// <summary>
        /// 获取某表的某个字段的最大值
        /// </summary>
        /// <param name="FieldName">字段名</param>
        /// <param name="TableName">表明</param>
        /// <returns>返回最大值</returns>
        public static int GetMaxID(string FieldName, string TableName)
        {
            string strsql = "select max(" + FieldName + ")+1 from " + TableName;
            object obj = SqlHelper.GetSingle(strsql);
            if (obj == null)
            {
                return 1;
            }
            else
            {
                return int.Parse(obj.ToString());
            }
        }
        /// <summary>
        /// 执行一条计算查询结果语句,返回查询结果(object)。
        /// </summary>
        /// <param name="SQLString">计算查询结果语句</param>
        /// <returns>查询结果(object)</returns>
        public static object GetSingle(string SQLString)
        {
            using (SqlCommand cmd = new SqlCommand(SQLString, GetConn()))
            {
                try
                {
                    object obj = cmd.ExecuteScalar();
                    if ((Object.Equals(obj, null)) || (Object.Equals(obj, System.DBNull.Value)))
                    {
                        return null;
                    }
                    else
                    {
                        return obj;
                    }
                }
                catch (System.Data.SqlClient.SqlException e)
                {
                    throw e;
                }
            }

        }

        /// <summary>
        /// 带参数返回一行一列ExecuteScalar
        /// </summary>
        /// <param name="cmdtext">存储过程或者SQL语句</param>
        /// <param name="para">参数数组</param>
        /// <param name="ct">命令类型</param>
        /// <returns>返回一行一列value</returns>
        public static int ExecuteScalar(string cmdtext, SqlParameter[] para, CommandType ct)
        {
            int value;
            try
            {
                cmd = new SqlCommand(cmdtext, GetConn());
                cmd.CommandType = ct;
                cmd.Parameters.AddRange(para);
                value = Convert.ToInt32(cmd.ExecuteScalar());
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                if (cn.State == ConnectionState.Open)
                {
                    cn.Close();
                }
            }
            return value;
        }
        /// <summary>
        /// 执行无参的操作
        /// </summary>
        /// <param name="cmdtext">SQL语句或存储过程</param>
        /// <