关于asp.net里数据库的移动

yetianbiao 2008-12-24 01:53:53
我写了这么一个,能吧数据库里的第一条赋值给各个textbox,还想加上一条,下一条,首,尾这四个按钮,我点击相应的按钮,textbox中的数据也能相应改变,应该怎么做啊?有人能教教我吗?


public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{

if (!IsPostBack)
{

bjd();
}
}
public void bjd()
{


string myStc = ConfigurationManager.AppSettings["ConnectionString"].ToString();
SqlConnection conn5 = new SqlConnection(myStc);
conn5.Open();

SqlCommand cmd5 = new SqlCommand("select * from tb_bjd", conn5);

SqlDataReader sdr5 = cmd5.ExecuteReader();
sdr5.Read();

if (sdr5["cpht"].ToString() == "是")
this.cpht.Checked = true;
else
this.cpht.Checked = false;

this.TextBox1.Text = sdr5["bjd_no"].ToString();
this.TextBox2.Text = sdr5["kh_no"].ToString();
this.TextBox3.Text = sdr5["kh_adress"].ToString();
this.TextBox4.Text = sdr5["kh_tel"].ToString();
this.TextBox5.Text = sdr5["zbj"].ToString();
this.YYSimpleCalendar2.Text = sdr5["kd_date"].ToString();
this.TextBox6.Text = sdr5["kh_name"].ToString();
this.TextBox7.Text = sdr5["kh_linkman"].ToString();
this.TextBox8.Text = sdr5["kh_fax"].ToString();
this.zk.Text = sdr5["z_zk"].ToString();
this.YYSimpleCalendar1.Text = sdr5["sh_date"].ToString();


conn5.Close();


}

...全文
158 28 打赏 收藏 转发到动态 举报
写回复
用AI写文章
28 条回复
切换为时间正序
请发表友善的回复…
发表回复
yetianbiao 2008-12-25
  • 打赏
  • 举报
回复
终于都搞定了~~太感谢了~
sprc_lcl 2008-12-25
  • 打赏
  • 举报
回复
DataTable dt = ds.Table[0];
this.ViewState.Add("dt", dt);
this.ViewState.Add("showi", 0);
dt.Dispose();
yetianbiao 2008-12-25
  • 打赏
  • 举报
回复
我想再问一下哦~那个这段里面


///*************************此段读到datatable*******************
string myStc = ConfigurationManager.AppSettings["ConnectionString"].ToString();
SqlConnection conn = new SqlConnection(myStc);
conn.Open();
string strSql = "select * from tb_bjd";
SqlDataAdapter adp = new SqlDataAdapter(strSql, conn);
DataSet ds = new DataSet();
adp.Fill(ds, "tb_bjd");
///***********************************************************************************************

this.ViewState.Add("dt", dt);
this.ViewState.Add("showi", 0);
dt.Dispose();


其中dt应该哪里定义啊?总说找不到dt~
sprc_lcl 2008-12-24
  • 打赏
  • 举报
回复
public partial class _Default : System.Web.UI.Page 
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
///*************************此段读到datatable手打,不知道对错*******************
string myStc = ConfigurationManager.AppSettings["ConnectionString"].ToString();
SqlConnection conn = new SqlConnection(myStc);
conn.Open();
strSql = "select * from tb_bjd";
SqlDataAdapter adp = new SqlDataAdapter(strSql, conn);
DataSet ds = new DataSet();
adp.Fill(ds, "tb_bjd");
///***********************************************************************************************

this.ViewState.Add("dt", dt);
this.ViewState.Add("showi", 0);
dt.Dispose();
bjd();
}
}

//绑定
public void bjd()
{
DataTable dt = (DataTable)this.ViewState["dt"];
int showi = (int)this.ViewState["showi"];
if (showi >= 0 && showi < dt.Rows.Count)
{
DataRow sdr5 = dt.Rows[showi];

if (sdr5["cpht"].ToString() == "是")
this.cpht.Checked = true;
else
this.cpht.Checked = false;

this.TextBox1.Text = sdr5["bjd_no"].ToString();
this.TextBox2.Text = sdr5["kh_no"].ToString();
this.TextBox3.Text = sdr5["kh_adress"].ToString();
this.TextBox4.Text = sdr5["kh_tel"].ToString();
this.TextBox5.Text = sdr5["zbj"].ToString();
this.YYSimpleCalendar2.Text = sdr5["kd_date"].ToString();
this.TextBox6.Text = sdr5["kh_name"].ToString();
this.TextBox7.Text = sdr5["kh_linkman"].ToString();
this.TextBox8.Text = sdr5["kh_fax"].ToString();
this.zk.Text = sdr5["z_zk"].ToString();
this.YYSimpleCalendar1.Text = sdr5["sh_date"].ToString();

}
dt.Dispose();
}
//下一行
private void nextr()
{
int showi = (int)this.ViewState["showi"] + 1;
DataTable dt = (DataTable)this.ViewState["dt"];
if (showi >= dt.Rows.Count)
{
showi = dt.Rows.Count - 1;
}
dt.Dispose();
this.ViewState.Add("showi", showi);
bjd();
}
//前一行
private void beforer()
{
int showi = (int)this.ViewState["showi"] - 1;
if (showi < 0)
{
showi = 0;
}
this.ViewState.Add("showi", showi);
bjd();
}
//首行
private void firstr()
{
this.ViewState.Add("showi", 0);
bjd();
}
//末行
private void lastr()
{
DataTable dt = (DataTable)this.ViewState["dt"];
this.ViewState.Add("showi", dt.Rows.Count-1);
dt.Dispose();
bjd();
}

protected void nextBtn_Click(object sender, EventArgs e)
{
nextr();
}
}

sundashuang 2008-12-24
  • 打赏
  • 举报
回复
帮顶!
yetianbiao 2008-12-24
  • 打赏
  • 举报
回复
我这么写了~~还是不对啊~~对于datatable~我不是很了解啊~~


public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{

if (!IsPostBack)
{
string myStc = ConfigurationManager.AppSettings["ConnectionString"].ToString();
SqlConnection conn5 = new SqlConnection(myStc);
conn5.Open();

//SqlCommand cmd5 = new SqlCommand("select * from tb_bjd", conn5);
DataTable dt = new DataTable("select * from tb_bjd");//读出来datatable,不要datareader

this.ViewState.Add("dt", dt);
this.ViewState.Add("showi", 0);
dt.Dispose();
conn5.Close();
bjd();
bjd_mx();
gvSearchBind();
}
}
public void bjd()
{


string myStc = ConfigurationManager.AppSettings["ConnectionString"].ToString();
SqlConnection conn5 = new SqlConnection(myStc);
conn5.Open();

SqlCommand cmd5 = new SqlCommand("select * from tb_bjd", conn5);

//SqlDataReader sdr5 = cmd5.ExecuteReader();
// sdr5.Read();
DataTable dt = (DataTable)this.ViewState["dt"];
int showi = (int)this.ViewState["showi"];
if (showi >= 0 && showi < dt.Rows.Count)
{
DataRow sdr5 = dt.Rows[showi];


// this.DropDownList1.Items.Add(new ListItem(sdr5["typeName"].ToString(), sdr5["typeName"].ToString()));


if (sdr5["cpht"].ToString() == "是")
this.cpht.Checked = true;
else
this.cpht.Checked = false;

this.TextBox1.Text = sdr5["bjd_no"].ToString();
this.TextBox2.Text = sdr5["kh_no"].ToString();
this.TextBox3.Text = sdr5["kh_adress"].ToString();
this.TextBox4.Text = sdr5["kh_tel"].ToString();
this.TextBox5.Text = sdr5["zbj"].ToString();
this.YYSimpleCalendar2.Text = sdr5["kd_date"].ToString();
this.TextBox6.Text = sdr5["kh_name"].ToString();
this.TextBox7.Text = sdr5["kh_linkman"].ToString();
this.TextBox8.Text = sdr5["kh_fax"].ToString();
this.zk.Text = sdr5["z_zk"].ToString();
this.YYSimpleCalendar1.Text = sdr5["sh_date"].ToString();

}
dt.Dispose();

conn5.Close();


}

public void bjd_mx()
{


string myStc = ConfigurationManager.AppSettings["ConnectionString"].ToString();
SqlConnection conn5 = new SqlConnection(myStc);
conn5.Open();

SqlCommand cmd5 = new SqlCommand("select * from bjd_zc", conn5);

SqlDataReader sdr5 = cmd5.ExecuteReader();
sdr5.Read();


// this.DropDownList1.Items.Add(new ListItem(sdr5["typeName"].ToString(), sdr5["typeName"].ToString()));




this.TextBox9.Text = sdr5["wp_no"].ToString();
this.TextBox16.Text = sdr5["wp_name"].ToString();
this.TextBox12.Text = sdr5["wp_num"].ToString();
this.TextBox13.Text = sdr5["page"].ToString();
this.TextBox11.Text = sdr5["wp_jg"].ToString();
this.TextBox17.Text = sdr5["page_jr"].ToString();
this.TextBox14.Text = sdr5["cp_zk"].ToString();
this.YYSimpleCalendar3.Text = sdr5["cp_jh_date"].ToString();



conn5.Close();


}

public void gvSearchBind()
{

string strSql;
string myStc = ConfigurationManager.AppSettings["ConnectionString"].ToString();
SqlConnection conn = new SqlConnection(myStc);
conn.Open();
strSql = "select * from bjd_zc where bjd_no='"+TextBox1.Text+"'";
SqlDataAdapter adp = new SqlDataAdapter(strSql, conn);
DataSet ds = new DataSet();
adp.Fill(ds, "bjd_zc");
GridView1.DataSource = ds.Tables["bjd_zc"].DefaultView;
GridView1.DataBind();
}

protected void piorBtn_Click(object sender, EventArgs e)
{


}
protected void nextBtn_Click(object sender, EventArgs e)
{
int showi = (int)this.ViewState["showi"] + 1;
DataTable dt = (DataTable)this.ViewState["dt"];
if (showi >= dt.Rows.Count)
{
showi = dt.Rows.Count - 1;
}
dt.Dispose();
this.ViewState.Add("showi", showi);

}
}

zilong32 2008-12-24
  • 打赏
  • 举报
回复
[Quote=引用 19 楼 koukoujiayi 的回复:]
将数据读到DataSet中,用DataSet中的Table来获取数据,这是可以的!!


C# code
public partial class MyPrograme : System.Web.UI.Page
{
DataSet ds;
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
ViewState["currentNo"] = 0;//保留当前的行号

SqlConnection myConn = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnStri…
[/Quote]
journeydj 2008-12-24
  • 打赏
  • 举报
回复
也是分页啊,只不过每页一条而已。sql分页,按钮只是传递的参数不一样而已。
takako_mu 2008-12-24
  • 打赏
  • 举报
回复
[Quote=引用 13 楼 softking1 的回复:]
8楼的可行
[/Quote]
8樓的是不可行的。
當你有幾次數據的時候你就要打開幾次連接,這樣很慢的。。
你可以一次性的讀出你的資料,用“~”(或者其他符號)來做隔離,將其賦給一個隱藏控件,用split切割給一個數組,這樣就可以讀啦~~~~
koukoujiayi 2008-12-24
  • 打赏
  • 举报
回复
将数据读到DataSet中,用DataSet中的Table来获取数据,这是可以的!!


public partial class MyPrograme : System.Web.UI.Page
{
DataSet ds;
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
ViewState["currentNo"] = 0;//保留当前的行号

SqlConnection myConn = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnString"].ConnectionString);
SqlDataAdapter da = new SqlDataAdapter("select * from youTable", myConn);
ds = new DataSet();
da.Fill(ds);
}


//上一条
protected void Button1_Click(object sender, EventArgs e)
{
if (Convert.ToInt16(ViewState["currentNo"]) == 0)
TextBox1.Text = ds.Tables[0].Rows[0]["id"].ToString();
else
{
TextBox1.Text = ds.Tables[0].Rows[Convert.ToInt16(ViewState["currentNo"]) + -1]["id"].ToString();
ViewState["currentNo"] = Convert.ToInt16(ViewState["currentNo"]) - 1;//保留当前的行号
}

}
//下一条
protected void Button2_Click(object sender, EventArgs e)
{

TextBox1.Text = ds.Tables[0].Rows[Convert.ToInt16(ViewState["currentNo"])+1]["id"].ToString();
ViewState["currentNo"] = Convert.ToInt16(ViewState["currentNo"])+1;//保留当前的行号
}
}
sprc_lcl 2008-12-24
  • 打赏
  • 举报
回复
web内不要用static设变量,除非就你自己用这个网站
sprc_lcl 2008-12-24
  • 打赏
  • 举报
回复
 if (!IsPostBack)
{
DataTable dt = new DataTable();//读出来datatable,不要datareader
this.ViewState.Add("dt", dt);
this.ViewState.Add("showi", 0);
dt.Dispose();
bjd();
}
}

private void bjd()
{
DataTable dt = (DataTable)this.ViewState["dt"];
int showi = (int)this.ViewState["showi"];
if (showi >= 0 && showi < dt.Rows.Count)
{
DataRow dr = dt.Rows[showi];
this.txtFied1.Text = dr["kh_adress"].ToString();
//..........
}
dt.Dispose();
}
private void nextr()
{
int showi = (int)this.ViewState["showi"] + 1;
DataTable dt = (DataTable)this.ViewState["dt"];
if (showi >= dt.Rows.Count)
{
showi = dt.Rows.Count - 1;
}
dt.Dispose();
this.ViewState.Add("showi", showi);
}
private void beforer()
{
int showi = (int)this.ViewState["showi"] - 1;
if (showi < 0)
{
showi = 0;
}
this.ViewState.Add("showi", showi);
}
BernardSun 2008-12-24
  • 打赏
  • 举报
回复

public static DataTable dt;
public static int i=0;

protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
dt= DBAccess.ExecuteQuery_Table("SELECT * FROM tb_bjd");
}
}

protected void Button1_Click(object sender, EventArgs e)
{
i = 0;
Label1.Text = dt.Rows[i][1].ToString();
}
protected void Button2_Click(object sender, EventArgs e)
{
i ++;
Label1.Text = dt.Rows[i][1].ToString();
}
protected void Button3_Click(object sender, EventArgs e)
{
i--;
Label1.Text = dt.Rows[i][1].ToString();
}
protected void Button4_Click(object sender, EventArgs e)
{
i = dt.Rows.Count-1;
Label1.Text = dt.Rows[i][1].ToString();
}

这样就可以,只是个示例,写的不完善有些地方要加些判断,自己加吧
yetianbiao 2008-12-24
  • 打赏
  • 举报
回复
[Quote=引用 8 楼 fxcjy 的回复:]
在你的表中有没有ID列?如果有,可以放一个隐藏的控件,记录当前你这些TextBox中显示的是哪条记录,然后在首,上一条,下一条,尾这四个按钮中分别重新从数据库中根据ID读一条记录,再把值显示在TextBox中.
[/Quote]

没有ID列啊~~只有编号~~编号中英文都有的~无规则~也能做吗?
yetianbiao 2008-12-24
  • 打赏
  • 举报
回复
[Quote=引用 12 楼 zhxhdean 的回复:]
引用 7 楼 hongmaohouzi 的回复:
给你个建议:在数据库的表里,增加一个字段(test1),用来作为记录行,然后,通过你的按钮来控制这个记录行;

上一条:根据你当前记录行的test1的值,然后自动减一,再根据新的记录行test1,来查询数据,如果到第一条,就不再减一;
下一条:根据你当前记录行的test1的值,然后自动加一,再根据新的记录行来test1,查询数据,如果到最后一条,就不再加一;
首:查询第一个数据,记录行te…
[/Quote]

还是不太明白啊~~我刚开始学~~能再说详细点吗?
softking1 2008-12-24
  • 打赏
  • 举报
回复
8楼的可行
zhxhdean 2008-12-24
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 hongmaohouzi 的回复:]
给你个建议:在数据库的表里,增加一个字段(test1),用来作为记录行,然后,通过你的按钮来控制这个记录行;

上一条:根据你当前记录行的test1的值,然后自动减一,再根据新的记录行test1,来查询数据,如果到第一条,就不再减一;
下一条:根据你当前记录行的test1的值,然后自动加一,再根据新的记录行来test1,查询数据,如果到最后一条,就不再加一;
首:查询第一个数据,记录行test1,的值是最小的记录;
尾:查…
[/Quote]
如果有数据删除了?当前记录行是10,但记录行9的数据已经删除了。用你这样的自动加减就不行了吧?
用个大于或小于当前标识符就可以了,但要主要排序下。这样才能查询到正确的数据
yetianbiao 2008-12-24
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 hongmaohouzi 的回复:]
给你个建议:在数据库的表里,增加一个字段(test1),用来作为记录行,然后,通过你的按钮来控制这个记录行;

上一条:根据你当前记录行的test1的值,然后自动减一,再根据新的记录行test1,来查询数据,如果到第一条,就不再减一;
下一条:根据你当前记录行的test1的值,然后自动加一,再根据新的记录行来test1,查询数据,如果到最后一条,就不再加一;
首:查询第一个数据,记录行test1,的值是最小的记录;
尾:查…
[/Quote]


大概明白了,但怎么判断是否到最后一条啊?
wuyq11 2008-12-24
  • 打赏
  • 举报
回复
通过存储过程实现。传递类别和ID号,判断ID大于和小于,实现记录指针移动。
BernardSun 2008-12-24
  • 打赏
  • 举报
回复
这不能用SqlDataReader了,他无法返回的
用datatable试试,做成全局的根据行号来选择
加载更多回复(8)

62,269

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧