Add, delete, modify, and query Oracle databases (simple operation)

Source: Internet
Author: User

1. First fill in the database connection string in Webconfig

<ConnectionStrings>
<Add name = "Oraclecon" connectionString = "Data Source = database name; Persist Security Info = True; User ID = username; Password = Password; Unicode = True" providerName = "System. data. oracleClient "/>
</ConnectionStrings>

2. Step 2: Create a page and add the following code

Public partial class WebForm1: System. Web. UI. Page
{

/// // Connect to the database /////////////////////// ////////
String oraclestr = ConfigurationManager. ConnectionStrings ["oraclecon"]. ConnectionString;
Protected void Page_Load (object sender, EventArgs e)
{
If (! IsPostBack)
{
LoadDate ();
}
}

When the program is running, load the following method to display the data, and bind the 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 ();
}

, Add code ,,,,,,,,,,,,,,,,

Protected void button#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 ();
}
}

, Delete Code ,,,,,,,,

Protected void linkbutton#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 ();
}
}

, Edit code ,,,,,,,,,,,,,,

Protected void LinkButton2_Click (object sender, EventArgs e)
{
LinkButton btnEdit = sender as LinkButton;
Response. Redirect ("WebEdit. aspx? & Id = "+ btnEdit. CommandArgument); // question mark
}
}

3. Step 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"]; // assign the passed value to a variable;
Load ();
}
}
Public void Load () // display data
{
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.txt id. Text = dt. Rows [0] ["ID"]. ToString ();
This.txt name. Text = dt. Rows [0] ["SNAME"]. ToString ();
This.txt mobile. Text = dt. Rows [0] ["MOBILE"]. ToString ();
}

/////// Edit part /////////
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"); // Save the modified data and return it to the start page, the modified data is displayed on the start page.
}
}
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.