First, on the operation of the database
1 database connection
of two kinds:
a first: ancient method (more rigid, for flexible operation), i.e., to connect with the class OracleConnection
string mysqlstr = "user id = xal ; data source = XAL; XAL password = ";
the OracleConnection mycnn the OracleConnection new new = (mysqlstr);
mycnn.open ();
second: the new process (using more flexible), i.e. using OracleConnectoinStringBuilder class to connect
OracleConnectionStringBuilder OcnnStrB = new new OracleConnectionStringBuilder;
OCnnStrB.DataSource = "XAL";
OCnnStrB.UserID = "XAL";
OCnnStrB.Password = "XAL";
myCnn the OracleConnection new new = (OCnnStrB.ConnectionString);
myCnn.open ();
2.事务操作
myConn.open();
OracleCommand insertComm = new OracleCommand();
insertComm.Connection = myCnn;
insertComm.Transaction = myCnn.BeginTransaction();
try
{
事务操作语句;
insertComm.Transaction.Commit();
}
catch(exption ex)
{
insertComm.Transaction.Rollback();
MessageBox(ex.Message);
}
finally
{
myConn.close();
}
3. Create command parameter
Private the OracleParameter CreateOraParam (ParamName String, Object ParamValue)
{
the OracleParameter the OracleParameter the Result = new new ();
Result.ParameterName = ParamName;
IF (! ParamValue = null)
{
Result.Value = ParamValue;
}
the else
{
Result.Value DBNull.Value =;
}
return the Result;
}
in this case, when the operation of the database can be:
insertComm.CommandText = "TESTADODOTNET INSERT INTO (ID, NAME, of AGE, the PIC) values (: pID,: pName,: Page, : pPic) ";
insertComm.Parameters.Add(CreateOraParam("pID", (txtID.Text.Trim() != "") ? txtID.Text.Trim() : null));
insertComm.Parameters.Add(CreateOraParam("pName", (txtName.Text.Trim() != "") ? txtName.Text.Trim() : null));
insertComm.Parameters.Add(CreateOraParam("pAge", (txtAge.Text.Trim() != "") ? txtAge.Text.Trim() : null));
4. The browsing data sets (Example: The results are shown in comboBox1)
the OracleDataAdapter the OracleDataAdapter new new ODA = (selectCommand An);
the DataTable NewTable the DataTable new new = ();
oda.Fill (NewTable);
the foreach (in the DataRow newtable.Rows DR) / / total newtable.rows.count records
{
comboBox1.Items.Add (DR [0] .ToString ());
}
The digital input can be provided (Example: textBox1 now to enter a similar method as the only letters)
Private void textBox1_KeyPress (Object SENDER, KeyPressEventArgs E) // event attribute
{
e.Handled = ((! Char.IsNumber (e.KeyChar)) || ((Keys) e.KeyChar == Keys.Back));
}
Thrown when the window is closed 6.Form events: pop up a dialog box OK to exit the
Private void form1_FormClosing (Object SENDER, FormClosingEventArgs E)
{
IF (MessageBox.Show ( "whether to exit the system?", "OK", MessageBoxButtons.YesNo, MessageBoxIcon .Question) == DialogResult.Yes)
{
the e.Cancel = to false;
}
the else
{
the e.Cancel = to true;
}
}
Usage 7.OracleParameter the
first step: creating a command parameter
Private OracleParameter CreateOraParam (String ParamName, Object ParamValue)
{
OracleParameter the Result = new new OracleParameter ();
Result.ParameterName = ParamName;
IF (ParamValue = null!)
{
Result.Value = ParamValue;
}
the else
{
Result.Value = DBNull.Value;
}
return the Result;
}
Step two: write SQL statements and call the first step of parameters (for example :: pID a parameter representative of the call insertComm.Parameters.Add pID value)
insertComm.CommandText = "insert into TESTADODOTNET (ID, NAME, AGE, PIC) values (:pID, :pName, :pAge, :pPic)";
insertComm.Parameters.Add(CreateOraParam("pID", (txtID.Text.Trim() != "") ? txtID.Text.Trim() : null));
insertComm.Parameters.Add(CreateOraParam("pName", (txtName.Text.Trim() != "") ? txtName.Text.Trim() : null));
insertComm.Parameters.Add(CreateOraParam("pAge", (txtAge.Text.Trim() != "") ? txtAge.Text.Trim() : null));
Step 3: Add the binary stream of image fields pictureBox1 Page
// Create a byte array to IMAGE field assignment, fileLength refers to the size of the selected file
byte [] = tmpImage new new byte [fileLength];
// The byte memory array creation flow, then the flow will affect the operation of the content byte array of
the MemoryStream = new new curStream the MemoryStream (tmpImage);
// in the graphic display control to the stream need to enforce the format
pictureBox1.Image. Save (curStream, curImageFormat); // front curImageFormat specified image format
insertComm.Parameters.Add (CreateOraParam ( "pPic", tmpImage));
Reproduced in: https: //www.cnblogs.com/salonliudong/archive/2006/12/04/582107.html