关于数据库的问题,请高手指点。

llw529 2003-07-21 10:44:03
在应用程序中是不是改变dataset中的数据之后,还要进行修改数据源的操作,如何处理?另外,对于执行插入删除等的控件如textbox,是不是像查询一样要进行数据邦定?
...全文
74 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
llw529 2003-07-21
  • 打赏
  • 举报
回复
必须邦定以后,才能更新数据库?
lkk2073 2003-07-21
  • 打赏
  • 举报
回复

用类的对象来调用:
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

namespace jobstar_lkk.basicInfo
{
/// <summary>
/// countryList 的摘要说明。
/// </summary>
public class countryList : System.Web.UI.Page
{
protected System.Web.UI.WebControls.DataGrid dg_country;
protected System.Web.UI.WebControls.TextBox tb_countryname;
protected System.Web.UI.WebControls.Button bt_countryfind;
protected System.Web.UI.WebControls.Button bt_add;
protected System.Web.UI.WebControls.Label lkkkkkk;
protected System.Web.UI.WebControls.TextBox tb_delete;
protected System.Web.UI.WebControls.Button bt_delete;
protected System.Web.UI.WebControls.TextBox tb_add;
protected System.Web.UI.WebControls.TextBox tb_amend;
protected System.Web.UI.WebControls.Button bt_amend;
DataSet ds_country=new DataSet();
module.Country cs_country=new module.Country();
module.Country.CountryDetail cs_country1=new module.Country.CountryDetail();

private void Page_Load(object sender, System.EventArgs e)
{
if(IsPostBack==false)
{
if(Session["id"]==null)
Response.Redirect("../default.aspx");
ds_country=cs_country.get_list("id>0");
dg_country.DataSource = ds_country;
dg_country.DataBind();

if(Request.QueryString["id"] != null )
{
module.Country.CountryDetail ds_country1=new module.Country .CountryDetail(System.Convert.ToInt32(Request.QueryString.Get("id")));
//tb_delete.Visible =true;
//bt_delete.Visible =true;
tb_amend.Visible =true;
bt_amend.Visible =true;
tb_delete.Text=ds_country1.get_name;
tb_amend.Text =ds_country1.get_name;
}
}
}

#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN:该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}

/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.bt_countryfind.Click += new System.EventHandler(this.bt_countryfind_Click);
this.bt_add.Click += new System.EventHandler(this.bt_add_Click);
this.bt_delete.Click += new System.EventHandler(this.bt_delete_Click);
this.bt_amend.Click += new System.EventHandler(this.bt_amend_Click);
this.Load += new System.EventHandler(this.Page_Load);

}
#endregion

private void bt_add_Click(object sender, System.EventArgs e)
{
module.Country.CountryDetail cs_country1=new module.Country.CountryDetail();
bool b;
tb_add.Visible =!tb_add.Visible;
b=tb_add.Visible;

if(b)
{
tb_add.Visible=true;
bt_add.Text ="保存添加";
}
else
{
cs_country1.get_name=tb_add.Text;
cs_country1.SB_insert();
bt_add.Text ="添加";
Response.Redirect("./countryList.aspx");
}
}

//分页时调用的函数
protected void dg_country_PageChage(object sender,DataGridPageChangedEventArgs e)
{

dg_country.SelectedIndex = -1;
dg_country.CurrentPageIndex = e.NewPageIndex;

}

private void bt_countryfind_Click(object sender, System.EventArgs e)
{
ds_country=cs_country.get_list(" name like "+"'"+"%"+tb_countryname.Text+"%"+"'");
dg_country.DataSource = ds_country;
dg_country.DataBind();
}

private void bt_delete_Click(object sender, System.EventArgs e)
{
module.Country.CountryDetail ds_country2=new module.Country .CountryDetail(System.Convert.ToInt32(Request.QueryString.Get("id")));
ds_country2.get_name=tb_delete.Text;
ds_country2.SB_delete();
Response.Redirect("./countryList.aspx");

}

private void bt_amend_Click(object sender, System.EventArgs e)
{
module.Country.CountryDetail ds_country3=new module.Country .CountryDetail(System.Convert.ToInt32(Request.QueryString.Get("id")));
ds_country3.get_name=tb_amend.Text;
ds_country3.SB_update();
Response.Redirect("./countryList.aspx");
}
}
}
lkk2073 2003-07-21
  • 打赏
  • 举报
回复
最好把对数据库的操作封装在类中,用对象来调用,以C#为例:


//对数据库的操作封装在类中
using System;
using System.Configuration;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;


namespace jobstar_lkk.module
{
public class Country
{
protected SqlConnection conn;
protected SqlCommand comm=new SqlCommand();
protected dbConn CoOp=new dbConn();
protected SqlDataAdapter coda;
protected DataSet cods=new DataSet();
protected string sqlword;

public DataSet get_list(string seekword)
{
conn = CoOp.getConnection();
comm =conn.CreateCommand();
if(seekword.Length!=0)
seekword=" and "+seekword;
sqlword = "select * from tb_country where id>0" + seekword;
coda = new SqlDataAdapter(sqlword, conn);
coda.Fill(cods,"Country");
conn.Close();
return cods;
}

public void fill_list(string seekword,DataSet fillds)
{
conn = CoOp.getConnection();
comm.Connection = conn;
if(seekword.Length!=0)
seekword=" and "+seekword;
sqlword = "select * from tb_country where id>0 " + seekword;
coda = new SqlDataAdapter(sqlword, conn);
coda.Fill(fillds,"Country");
conn.Close();
}

public class CountryDetail //国家表详细内容
{
private int id;
protected SqlConnection conn;
protected SqlCommand comm=new SqlCommand();
protected dbConn CoOp=new dbConn();
protected SqlDataAdapter coda;
protected DataSet cods=new DataSet();
protected string sqlword,name;

public string get_name
{
get
{
return name;
}
set
{
name=value;
}
}

public int get_id
{
get
{
return id;
}
set
{
id=value;
}
}

public void get_ds()
{
conn = CoOp.getConnection();
comm.Connection = conn;
sqlword = "select * from tb_country where id=" +id;
if(id>0)
{
coda = new SqlDataAdapter(sqlword, conn);
coda.Fill(cods, "Country");
DataRow newRow= cods.Tables["Country"].Rows[0];
name=newRow["name"].ToString();
}
}

public void SB_insert()
{
conn = CoOp.getConnection();
comm.Connection = conn;
sqlword = "insert into tb_country values("+ "'"+name+"'" +")";
comm.CommandText = sqlword;
comm.Connection.Open();
comm.ExecuteNonQuery();
comm.Connection.Close();
}

public void SB_update()
{
conn = CoOp.getConnection();
comm.Connection = conn;
sqlword = "update tb_country set name="+"'"+ name+"'" +" where id=" +id;
comm.CommandText = sqlword;
comm.Connection.Open();
comm.ExecuteNonQuery();
comm.Connection.Close();
}

public void SB_delete()
{
conn = CoOp.getConnection();
comm.Connection = conn;
sqlword = "delete tb_country where id=" +id;
comm.CommandText = sqlword;
comm.Connection.Open();
comm.ExecuteNonQuery();
comm.Connection.Close();
}

public CountryDetail()
{
id=0;
}

public CountryDetail(int _id)
{
id=_id;
get_ds();
}
}
}
}
HenanBoy 2003-07-21
  • 打赏
  • 举报
回复
呵呵~~~必须对控件绑定以后,的才能用oleDbDataAdapter1.Update,SqlDataAdpater1.Update
lbx1979 2003-07-21
  • 打赏
  • 举报
回复
对,用dataadapter.update(dataset)方法更新数据库
redant0 2003-07-21
  • 打赏
  • 举报
回复
1.需要,执行oleDbDataAdapter1.Update;
2.如果你要在textbox中进行新增或修改,则需要绑定

110,538

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

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