一、關於資料庫的操作
1.資料庫連接
有2種:
第一種:古老的方法(較為死板,不利於靈活操作),即用OracleConnection的類來串連
string mysqlstr ="user id = xal;data source = xal;password = xal";
OracleConnection mycnn = new OracleConnection(mysqlstr);
mycnn.open();
第二種:新式的方法(使用較為靈活),即利用OracleConnectoinStringBuilder類來串連
OracleConnectionStringBuilder OcnnStrB = new OracleConnectionStringBuilder;
OCnnStrB.DataSource = "xal";
OCnnStrB.UserID = "xal";
OCnnStrB.Password = "xal";
myCnn = new OracleConnection(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.建立命令參數
private OracleParameter CreateOraParam(string ParamName, object ParamValue)
{
OracleParameter Result = new OracleParameter();
Result.ParameterName = ParamName;
if (ParamValue != null)
{
Result.Value = ParamValue;
}
else
{
Result.Value = DBNull.Value;
}
return Result;
}
這樣的話,當要對資料庫操作時就可以:
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));
4.資料集的瀏覽(例:將結果顯示在comboBox1中)
OracleDataAdapter oda = new OracleDataAdapter(selectCommand);
DataTable newtable = new DataTable();
oda.Fill(newtable);
foreach (DataRow dr in newtable.Rows) //共有newtable.rows.count條記錄
{
comboBox1.Items.Add(dr[0].ToString());
}
5.設定輸入只能是數字(例:現在往textBox1中輸入。如只能輸入字母的方法類似)
private void textBox1_KeyPress(object sender, KeyPressEventArgs e)//屬性中的事件
{
e.Handled = !((Char.IsNumber(e.KeyChar)) || ((Keys)e.KeyChar == Keys.Back));
}
6.Form視窗關閉時引發的事件:彈出一個確定退出的對話方塊
private void form1_FormClosing(object sender, FormClosingEventArgs e)
{
if (MessageBox.Show("是否退出系統?", "確認", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
{
e.Cancel = false;
}
else
{
e.Cancel = true;
}
}
7.OracleParameter的用法
第一步:先建立命令參數
private OracleParameter CreateOraParam(string ParamName, object ParamValue)
{
OracleParameter Result = new OracleParameter();
Result.ParameterName = ParamName;
if (ParamValue != null)
{
Result.Value = ParamValue;
}
else
{
Result.Value = DBNull.Value;
}
return Result;
}
第二步:寫SQL語句,並調用第一步的參數(例如::pID是個參數,代表調用insertComm.Parameters.Add中的pID的值)
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));
第三步:添加pictureBox1圖片的二進位流欄位pAge
//建立位元組數組用於給IMAGE欄位賦值,fileLength是指所選的檔案的大小
byte[] tmpImage = new byte[fileLength];
//根據位元組數組建立記憶體流,之後對該流的操作將會影響位元組數組的內容
MemoryStream curStream = new MemoryStream(tmpImage);
//把控制項內顯示的圖形寫入到流中,需強制指定格式
pictureBox1.Image.Save(curStream, curImageFormat);//curImageFormat前面指定的圖片格式
insertComm.Parameters.Add(CreateOraParam("pPic", tmpImage));