.net C#如何能在一个gridview的模板 里面显示多个特定的条目?
for(j=1;j<6;j++)//我希望显示的是id为1至5,但显示的只是id为5{strings="select*fromBlankwhereid='"+j+"'";...
for (j = 1; j < 6; j++)//我希望显示的是id为1至5,但显示的只是id为5
{
string s = "select * from Blank where id ='" + j + "'";
SqlDataAdapter sda = new SqlDataAdapter(s, conn);
DataSet ds = new DataSet();
sda.Fill(ds);
GridView1.DataSource = ds;
GridView1.DataBind();
}
// if (!IsPostBack)
{
conn.Open();
SqlCommand cmdjudge = new SqlCommand("select * from Blank where id ='" + txtblank[j] + "'", conn);
SqlDataReader sdrjudge = cmdjudge.ExecuteReader();
while (sdrjudge.Read())
{
string strjudge = "select * from Blank where id ='" + txtblank[j] + "'";
BaseClass.BindDG(GridView1, "id", strjudge, "Blank");
// Label1.Text = sdrjudge["blankcontent"].ToString();
}
conn.Close();
}
}
错了……发错了,代码只是到ispostback前面……后面发错了…… 展开
{
string s = "select * from Blank where id ='" + j + "'";
SqlDataAdapter sda = new SqlDataAdapter(s, conn);
DataSet ds = new DataSet();
sda.Fill(ds);
GridView1.DataSource = ds;
GridView1.DataBind();
}
// if (!IsPostBack)
{
conn.Open();
SqlCommand cmdjudge = new SqlCommand("select * from Blank where id ='" + txtblank[j] + "'", conn);
SqlDataReader sdrjudge = cmdjudge.ExecuteReader();
while (sdrjudge.Read())
{
string strjudge = "select * from Blank where id ='" + txtblank[j] + "'";
BaseClass.BindDG(GridView1, "id", strjudge, "Blank");
// Label1.Text = sdrjudge["blankcontent"].ToString();
}
conn.Close();
}
}
错了……发错了,代码只是到ispostback前面……后面发错了…… 展开
2个回答
展开全部
DataSet 被覆盖 只是写入了最后一条数据,前面被覆盖调了
DataSet ds = new DataSet();//定义到方法 外面
for (j = 1; j < 6; j++)//我希望显示的是id为1至5,但显示的只是id为5
{
string s = "select * from Blank where id ='" + j + "'";
SqlDataAdapter sda = new SqlDataAdapter(s, conn);
DataSet ds = new DataSet();/
sda.Fill(ds);
GridView1.DataSource = ds;
GridView1.DataBind();
}
最好这样写(避免多次连接数据库 提升性能)
{
string s = "select * from Blank where id in (1,2,3,4,5)";
SqlDataAdapter sda = new SqlDataAdapter(s, conn);
DataSet ds = new DataSet();
sda.Fill(ds);
GridView1.DataSource = ds;
GridView1.DataBind();
}
DataSet ds = new DataSet();//定义到方法 外面
for (j = 1; j < 6; j++)//我希望显示的是id为1至5,但显示的只是id为5
{
string s = "select * from Blank where id ='" + j + "'";
SqlDataAdapter sda = new SqlDataAdapter(s, conn);
DataSet ds = new DataSet();/
sda.Fill(ds);
GridView1.DataSource = ds;
GridView1.DataBind();
}
最好这样写(避免多次连接数据库 提升性能)
{
string s = "select * from Blank where id in (1,2,3,4,5)";
SqlDataAdapter sda = new SqlDataAdapter(s, conn);
DataSet ds = new DataSet();
sda.Fill(ds);
GridView1.DataSource = ds;
GridView1.DataBind();
}
追问
谢谢啦!!!搞定了!!DataSet 确实被覆盖了!!
展开全部
楼上的仁兄,你有点误人子弟啊。。。
// 不需要循环的,查询条件改为 id BETWEEN 1 AND 5
var ds = new DataSet();
var s = string.Format("SELECT * FROM [Blank] WHERE id BETWEEN {0} AND {1}", 1, 5);
var adapter = new SqlDataAdapter(s, conn);
adapter.Fill(ds);
GridView1.DataSource = ds;
GridView1.DataBind();
// 不需要循环的,查询条件改为 id BETWEEN 1 AND 5
var ds = new DataSet();
var s = string.Format("SELECT * FROM [Blank] WHERE id BETWEEN {0} AND {1}", 1, 5);
var adapter = new SqlDataAdapter(s, conn);
adapter.Fill(ds);
GridView1.DataSource = ds;
GridView1.DataBind();
追问
,
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询