数据库
首页 > 数据库> > SQL Server存储过程:数据统计和C#使用方式

SQL Server存储过程:数据统计和C#使用方式

作者:互联网

根据条件统计维度存储过程

CREATE PROCEDURE [dbo].[Proc_WeiDuTongJi]
	@code VARCHAR(9)
AS
 
BEGIN

	declare @weidu1 int,@weidu2 int,@weidu3 int,@weidu4 int,@weidu5 int

	select @weidu1=0,@weidu2 =0,@weidu3 =0,@weidu4 =0,@weidu5 =0
	
	--维度1
	select @weidu1=count(0) from [Table1](nolock) where [code]=@code
	
	--维度2
	select @weidu2=count(0) from [Table2](nolock) where [code]=@code
	
	--维度3
	select @weidu3=count(0) from [Table3](nolock) where [code]=@code
	
	--维度4
	select @weidu4=count(0) from [Table4](nolock) where [code]=@code
	
	--维度5
	select @weidu5=count(0) from [Table5](nolock) where [code]=@code
	
	select @weidu1 as 'weidu1',@weidu2 as 'weidu2',@weidu3 as 'weidu3',@weidu4 as 'weidu4',
	@weidu5 as 'weidu5'

END
GO

通过C#获取统计维度


        public cla GetWeiDu()
        {
            cla item = null;
            SqlParameter[] sps = new SqlParameter[] {
                new SqlParameter("@code","1111111111")
            };
            
			//传入需要统计的条件code
            SqlDataReader dataReader = ExecuteProcReader("Proc_WeiDuTongJi", sps);

            if (dataReader.Read())
            {
                item = new cla();
                item.a = Convert.ToInt32(dataReader["weidu1"]).ToString();
                item.b = Convert.ToInt32(dataReader["weidu2"]).ToString();
                item.c = Convert.ToInt32(dataReader["weidu3"]).ToString();
                item.d = Convert.ToInt32(dataReader["weidu4"]).ToString();
                item.e = Convert.ToInt32(dataReader["weidu5"]).ToString();
            }
            dataReader.Close();
            return item;
        }

        /// <summary>
        /// 执行存储过程
        /// </summary>
        /// <param name="procName"></param>
        /// <param name="param"></param>
        /// <returns>记录集</returns>
        public SqlDataReader ExecuteProcReader(string procName, SqlParameter[] param)
        {
            SqlConnection conn = new SqlConnection("server=192.168.2.168;database=table;uid=admin;pwd=123456");
            SqlCommand cmd = new SqlCommand(procName, conn);
            cmd.Parameters.AddRange(param);
            cmd.CommandType = CommandType.StoredProcedure;
            conn.Open();
            return cmd.ExecuteReader(CommandBehavior.CloseConnection);
        }

标签:code,C#,weidu4,Server,item,dataReader,SQL,维度,select
来源: https://blog.csdn.net/q944468002/article/details/120923041