C # connect to oracle for addition, deletion, modification, and query

Source: Internet
Author: User
Take the users table as an example. There are three fields: Self-incrementing id, int type, name, nvarchar type, password pwd, and nvarchar type. System is first introduced in vs2005. data. oracleClient; namespace publicintInsert (stringname, stringpwd) {OracleConnectionconnnewOracleConnection (@ D

Take the users table as an example. There are three fields: Self-incrementing id, int type, name, nvarchar type, password pwd, and nvarchar type. System is first introduced in vs2005. data. oracleClient; namespace publicint Insert (stringname, string pwd) {OracleConnectionconn = new OracleConnection (@ D

Take the users table as an example. There are three fields: Self-incrementing id, int type, name, nvarchar type, password pwd, and nvarchar type.
First introduce System. Data. OracleClient; namespace in vs2005

Publicint Insert (stringname, string pwd)

{

OracleConnectionconn = new OracleConnection (@ "Data Source = SBZX; User ID = simis; Password = zeda ");

Conn. Open ();

Stringsql = "insert into users (name, pwd) values (: name,: pwd )";

OracleCommandcmd = new OracleCommand (SQL, conn );

OracleParameter parn = new OracleParameter (": name", name );

Cmd. Parameters. Add (parn );

OracleParameter parp = new OracleParameter (": pwd", name );

Cmd. Parameters. Add (parp );

Int result = cmd. ExecuteNonQuery (); // The number of affected rows received by result. If result is greater than 0, the addition is successful.

Conn. Close ();

Cmd. Dispose ();

Return result;

}

Publicint Update (int id)

{

OracleConnectionconn = new OracleConnection (@ "Data Source = SBZX; User ID = simis; Password = zeda ");

Conn. Open ();

Stringsql = "delete from users where id =: id ";

OracleCommandcmd = new OracleCommand (SQL, conn );

OracleParameterpari = new OracleParameter (": id", id );

Cmd. Parameters. Add (pari );

Int result = cmd. ExecuteNonQuery (); // The number of affected rows received by result. If result is greater than 0, the addition is successful.

Conn. Close ();

Cmd. Dispose ();

Return result;

}

Publicint Insert (stringname, string pwd, intid)

{

OracleConnectionconn = new OracleConnection (@ "Data Source = SBZX; User ID = simis; Password = zeda ");

Conn. Open ();

Stringsql = "update users set name =: name, pwd =: pwdwhere id =: id ";

OracleCommandcmd = new OracleCommand (SQL, conn );

OracleParameterparn = new OracleParameter (": name", name );

Cmd. Parameters. Add (parn );

OracleParameterparp = new OracleParameter (": pwd", name );

Cmd. Parameters. Add (parp );

OracleParameterpari = new OracleParameter (": id", id );

Cmd. Parameters. Add (pari );

Intresult = cmd. ExecuteNonQuery ();

Conn. Close ();

Cmd. Dispose ();

Returnresult;

}

PublicDataTable Select ()

{

OracleConnectionconn = new OracleConnection (@ "Data Source = SBZX; User ID = simis; Password = zeda ");

Conn. Open ();

Stringsql = "select * from users ";

OracleCommandcmd = new OracleCommand (SQL, conn );

OracleDataAdapteroda = new OracleDataAdapter (cmd );

DataTable dt = new DataTable ();

Oda. Fill (dt );

Conn. Close ();

Cmd. Dispose ();

Return dt;

}

After writing the method, the following is an example of a query, drag a dview in the form, and then in the Load method
    private void Form1_Load(object sender, EventArgs e)
        {
              dataGridView1.DataSource = Select();
        }
In this way, data is displayed in the DataGridView.

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.