Creat PROCEDURE [dbo].[abc] @s varchar(8000), @e varchar(8000) AS declare @sql1 varchar(8000) set @sql1 = 'select Name as n' select @sql1 = @sql1 + ',sum(case Subject when ''' + Subject + ''' then Result else 0 end) [' + Subject + ']' from (select distinct Subject from cj) as a set @sql1 ='select Name,sum(result) 总分,'+right(@sql1,len(@sql1)-17)+' from cj WHERE RESULT BETWEEN '+@s+' AND '+@e+' group by name' exec(@sql1) exec abc 70,80
using System.Data.SqlClient; using System.Configuration; using System.Data; protected void Button1_Click(object sender, EventArgs e) { using (SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["testConnectionString"].ConnectionString)) { conn.Open(); SqlDataAdapter sqldap = new SqlDataAdapter(); SqlCommand cmm = new SqlCommand(); cmm.Connection = conn; cmm.CommandType = CommandType.StoredProcedure; cmm.CommandText = "abc"; cmm.Parameters.Add("s", SqlDbType.VarChar); cmm.Parameters.Add("e", SqlDbType.VarChar); cmm.Parameters["s"].Value = TextBox1.Text; cmm.Parameters["e"].Value = TextBox2.Text; sqldap.SelectCommand = cmm; DataTable dt = new DataTable(); sqldap.Fill(dt); GridView2.DataSource = dt; GridView2.DataBind(); } }
就这么处理!