make adapter获取多个语句 [英] make adapter gets multiple statement

查看:63
本文介绍了make adapter获取多个语句的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 SqlConnection con =  new  SqlConnection(ConfigurationManager.ConnectionStrings [  connection]。ConnectionString); 
SqlCommand com = new SqlCommand();
con.Open();
com.Connection = con;

// 获取此老师的课程ID
string cmd1 = 从course_teacher中选择course_id,其中teacher_id =' + Session [ t_id] + ';
SqlDataAdapter da1 = new SqlDataAdapter(cmd1,con);
DataSet ds1 = new DataSet();
da1.Fill(ds1);
string cmd2 = ;
for int i = 0 ; i < ds1.Tables [ 0 ]。Rows.Count; i ++)
{
int courseid = Convert.ToInt32(ds1.Tables [ 0 ]。行[ i] [ course_id]。ToString());
cmd2 + = 从课程中选择(course_name +' - '+ class_name)course_id =' + courseid + ';
}
SqlDataAdapter da2 = new SqlDataAdapter(cmd2,con);
DataSet ds2 = new DataSet();
da2.Fill(ds2);
DropDownList1.DataSource = ds2;
DropDownList1.DataBind();



问题是循环中的适配器只获取第一个语句...

解决方案

您好,



此代码:



 SqlDataAdapter da2 = new SqlDataAdapter(cmd2,con); 
DataSet ds2 = new DataSet();
da2.Fill(ds2);
DropDownList1.DataSource = ds2;
DropDownList1.DataBind();





在循环语句之外,这就是你无法得到的原因第二个适配器的结果。



干杯,

JAFC


以上所有你想拥有的数据数据集或数据表权利。所以请执行以下操作来解决您的问题



 //为此老师获取课程ID 
string cmd1 =select course_id来自course_teacher,其中teacher_id =''+ Session [t_id] +'';
SqlDataAdapter da1 = new SqlDataAdapter(cmd1,con);
DataSet ds1 = new DataSet();
da1.Fill(ds1);
string cmd2 =;
Datatable dt-new DataTable();
for(int i = 0; i< ds1.Tables [0] .Rows.Count; i ++)
{
int courseid = Convert.ToInt32(ds1.Tables [0]。行[I] [ COURSE_ID]的ToString());
cmd2 =select(course_name +'' - ''+ class_name)来自course_id =''+ courseid +''的课程;
SqlDataAdapter da2 = new SqlDataAdapter(cmd2,con);
DataSet ds2 = new DataSet();
da2.Fill(ds2);

if(i = 0)
{
dt = ds2.tables [0];
}
其他
{
dt.merge(ds2.tables [0]);
}
}

DropDownList1.DataSource = dt;
DropDownList1.DataBind();





我相信它会解决你的问题......


SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["connection"].ConnectionString);
SqlCommand com = new SqlCommand();
con.Open();
com.Connection = con;

//get course id's for this teacher
string cmd1 = "select course_id from course_teacher where teacher_id='" + Session["t_id"] + "'";
SqlDataAdapter da1 = new SqlDataAdapter(cmd1, con);
DataSet ds1 = new DataSet();
da1.Fill(ds1);
string cmd2 = "";
for (int i = 0; i < ds1.Tables[0].Rows.Count; i++)
{
int courseid = Convert.ToInt32(ds1.Tables[0].Rows[i]["course_id"].ToString());
cmd2 += "select (course_name+'-'+class_name)from courses where course_id='" + courseid + "'";
}
SqlDataAdapter da2 = new SqlDataAdapter(cmd2, con);
DataSet ds2 = new DataSet();
da2.Fill(ds2);
DropDownList1.DataSource = ds2;
DropDownList1.DataBind();


the problem is the adapter in the loop gets just the first statement...

解决方案

Hi,

This code block:

SqlDataAdapter da2 = new SqlDataAdapter(cmd2, con);
DataSet ds2 = new DataSet();
da2.Fill(ds2);
DropDownList1.DataSource = ds2;
DropDownList1.DataBind();



is outside for loop statement and that is the reason why you can''t get results from the second adapter.

Cheers,
JAFC


all of the above you want to have the data in a dataset or datatable right. so pls do the following which should solve your problem

//get course ids for this teacher
string cmd1 = "select course_id from course_teacher where teacher_id=''" + Session["t_id"] + "''";
SqlDataAdapter da1 = new SqlDataAdapter(cmd1, con);
DataSet ds1 = new DataSet();
da1.Fill(ds1);
string cmd2 = "";
Datatable dt-new DataTable();
for (int i = 0; i < ds1.Tables[0].Rows.Count; i++)
{
int courseid = Convert.ToInt32(ds1.Tables[0].Rows[i]["course_id"].ToString());
cmd2 = "select (course_name+''-''+class_name)from courses where course_id=''" + courseid + "''";
SqlDataAdapter da2 = new SqlDataAdapter(cmd2, con);
DataSet ds2 = new DataSet();
da2.Fill(ds2);

if(i=0)
{
dt=ds2.tables[0];
}
else
{
dt.merge(ds2.tables[0]);
}
}

DropDownList1.DataSource = dt;
DropDownList1.DataBind();



and i am sure it will solve your problem.....


这篇关于make adapter获取多个语句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆