Oracle数据库的增删改查(简单操作)
1.首先在 Webconfig里面填写数据库连接字符串
<connectionStrings>
<add name="Oraclecon" connectionString="Data Source=数据库名称;Persist Security Info=True;User ID=用户名;Password=密码;Unicode=True" providername="System.Data.OracleClient"/>
</connectionStrings>
2.第二步,新建一个页面,添加下面的代码
public partial class WebForm1 : System.Web.UI.Page
{
///////////////连接数据库///////////////////////////////
string oraclestr = ConfigurationManager.ConnectionStrings["oraclecon"].ConnectionString;
protected void Page_Load(object sender, EventArgs e)
{
if(!IsPostBack)
{
LoadDate();
}
}
当程序时,加载下面的方法,将数据显示出来,绑定的是GridView;
public void LoadDate()
{
OracleConnection oracle = new OracleConnection(oraclestr);
oracle.Open();
OracleCommand cmd = new OracleCommand();
cmd.Connection = oracle;
cmd.CommandText = "select * from T_Student ORDER BY ID";
OracleDataAdapter adapter = new OracleDataAdapter(cmd);
DataTable dt = new DataTable();
adapter.Fill(dt);
cmd.Dispose();
oracle.Dispose();
this.GridView1.DataSource = dt;
this.GridView1.DataBind();
}
、、、、、、、、、、增加代码、、、、、、、、、、、、、、、、
protected void Button1_Click(object sender, EventArgs e)
{
OracleConnection oracle = new OracleConnection(oraclestr);
oracle.Open();
OracleCommand cmd = new OracleCommand();
cmd.Connection = oracle;
cmd.CommandText = "insert into T_Student (ID,SNAME,MOBILE)values(:id,:name,:mobile)";
cmd.Parameters.AddWithValue(":id",txtid.Text);
cmd.Parameters.AddWithValue(":name",txtname.Text);
cmd.Parameters.AddWithValue(":mobile",txtmobile.Text);
int numb = cmd.ExecuteNonQuery();
//cmd.Dispose();
//oracle.Dispose();
if(numb>0)
{
Response.Write("ok");
LoadDate();
}
}
、、、、、、、、删除代码、、、、、、、、
protected void LinkButton1_Click(object sender, EventArgs e)
{
LinkButton btndelete = sender as LinkButton;
OracleConnection oracle = new OracleConnection(oraclestr);
oracle.Open();
OracleCommand cmd = new OracleCommand();
cmd.Connection = oracle;
cmd.CommandText = "delete from T_Student where ID=:id";
cmd.Parameters.AddWithValue(":id",btndelete.CommandArgument);
int numb = cmd.ExecuteNonQuery();
if (numb > 0)
{
Response.Write("ok");
LoadDate();
}
}
、、、、、、、、、、编辑代码、、、、、、、、、、、、、、
protected void LinkButton2_Click(object sender, EventArgs e)
{
LinkButton btnEdit = sender as LinkButton;
Response.Redirect("WebEdit.aspx?&id="+btnEdit.CommandArgument);//////////////////问号传值
}
}
3.第三步:
public partial class WebEdit : System.Web.UI.Page
{
string oraclestr = ConfigurationManager.ConnectionStrings["oraclecon"].ConnectionString;
string id;
protected void Page_Load(object sender, EventArgs e)
{
if(!IsPostBack)
{
id=Request.QueryString["id"];//////////////把传过来的值赋给一个变量;
Load();
}
}
public void Load() ////////////显示数据
{
OracleConnection oracle = new OracleConnection(oraclestr);
oracle.Open();
OracleCommand cmd = new OracleCommand();
cmd.Connection = oracle;
cmd.CommandText = "select * from T_Student where ID=:id";
cmd.Parameters.AddWithValue(":id",id);
OracleDataAdapter adapter = new OracleDataAdapter(cmd);
DataTable dt = new DataTable();
adapter.Fill(dt);
cmd.Dispose();
oracle.Dispose();
this.txtid.Text = dt.Rows[0]["ID"].ToString();
this.txtname.Text=dt.Rows[0]["SNAME"].ToString();
this.txtmobile.Text=dt.Rows[0]["MOBILE"].ToString();
}
////////编辑部分/////////
protected void btnSave_Click(object sender, EventArgs e)
{
string id = txtid.Text;
string name = txtname.Text;
string mobile = txtmobile.Text;
OracleConnection oracle = new OracleConnection(oraclestr);
oracle.Open();
OracleCommand cmd = new OracleCommand();
cmd.Connection = oracle;
cmd.CommandText = "update T_Student SET SNAME=:NAME,MOBILE=:MOBILE where ID=:id";
cmd.Parameters.AddWithValue(":NAME",name);
cmd.Parameters.AddWithValue(":MOBILE",mobile);
cmd.Parameters.AddWithValue(":id",id);
int numb = cmd.ExecuteNonQuery();
if (numb > 0)
{
Response.Write("ok");
Response.Redirect("WebForm1.aspx");////////////////////将修改的数据,保存起来,再传回起始页面,并在起始页面显示修改的数据。
}
}
}