sql="select 学期课程,count(学期课程) as 参加人数,sum(score) as 总成绩,avg(score) as 平均成绩,max(score) as 最高分,min(score) as 最低分 from (select semester,'('+cstr(cint(semester/10))+'-'+cstr(cint(semester/10)+1)+'年度第'+cstr(semester mod 10)+'学期)'+scoreinfo.course as 学期课程,score from (scoreinfo inner join studentinfo on scoreinfo.studentnumber=studentinfo.studentnumber) inner join classinfo on studentinfo.classid=classinfo.classid where classinfo.classname='"+textBox1.Text.Trim()+"' and semester between '"+xq1+"'and '"+xq2+"' order by semester desc) as xqkc group by xqkc.学期课程";
这句话是用VB语言写的,如何改动可以让它在c#程序中可以使用?
SqlConnection cnd = new SqlConnection("data source=.;initial catalog=MySample;integrated security=SSPI");
cnd.Open();
//连接到数据库
SqlCommand cmd = new SqlCommand("select 学期课程,count(学期课程) as 参加人数,sum(score) as 总成绩,avg(score) as 平均成绩,max(score) as 最高分,min(score) as 最低分 from (select semester,'('+cstr(cint(semester/10))+'-'+cstr(cint(semester/10)+1)+'年度第'+cstr(semester mod 10)+'学期)'+scoreinfo.course as 学期课程,score from (scoreinfo inner join studentinfo on scoreinfo.studentnumber=studentinfo.studentnumber) inner join classinfo on studentinfo.classid=classinfo.classid where classinfo.classname='"+textBox1.Text.Trim()+"' and semester between '"+xq1+"'and '"+xq2+"' order by semester desc) as xqkc group by xqkc."", cnd);
SqlDataAdapter sda = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
sda.Fill(ds, "student");
SqlCommandBuilder scb = new SqlCommandBuilder(sda);
//生成表单
DataTable dt = ds.Tables["student"];
SqlDataReader sread;
//读取表
sread = cmd.ExecuteReader();
while (sread.Read())//赋值
{
}
cnd.Close();
SQL语句和其他语言是分开的,这句话不需要改动什么。
要改就只要改变量的用法,比如C#中,SQL语句中的变量是需要占位符的