回 帖 发 新 帖 刷新版面

主题:[讨论]关于在C#中利用双表关联的select查询语句怎么绑定dataGrid1呢

关于在C#中利用双表关联的select查询语句怎么绑定dataGrid1呢
以下是我写的程序,大家看看能不能修改下:
string str2 = String.Format("select studid as 学生编号,studscoreinfo.studno as 学生学号,studname,studscore as 学生成绩 from studscoreinfo,studinfowhere studscoreinfo.studno=studinfo.studno and studname='{0}'", textBox1.Text);
                    SqlDataAdapter sqladapter = new SqlDataAdapter(str2, cx.conn);
                    DataSet ds = new DataSet();
                   [color=800000] sqladapter.Fill(ds);
                    dataGrid1.DataSource = ds.Tables["studinfo"];[/color]
大家看看应该怎么改?!小弟急用

回复列表 (共5个回复)

沙发

select studid as 学生编号,studscoreinfo.studno as 学生学号,studname,studscore as 学生成绩 from studscoreinfo,studinfowhere studscoreinfo.studno=studinfo.studno and studname='{0}'
自己再好好看一下SELECT语句吧,少一个空格也有错噢。

板凳

我知道啊
但是我要将studscoreinfo和studinfo这两个表关联后显示出来,sqladapter.Fill(ds);
dataGrid1.DataSource = ds.Tables["studinfo"];这两句该怎么写呢

3 楼

sqladapter.Fill(ds);
dataGrid1.DataSource = ds.Tables["studinfo"];
这是填充和绑定的操作,不需要再写什么了。关键是SQL语句。

4 楼

如果是两表相联结应该为:
string str2 = String.Format("select studid as 学生编号,studscoreinfo.studno as 学生学号,studname,studscore as 学生成绩 from studscoreinfo inner join studinfo on studscoreinfo.studno=studinfo.studno where studname='{0}'", textBox1.Text);

5 楼

绑定还是一样的,就是sql语句的问题。你可以直接用Response.write()把sql语句结果输出,然后在查询分析器中运行就知道sql语句对错了。

我来回复

您尚未登录,请登录后再回复。点此登录或注册