C#中oracle数据库的连接方法
一、關于數據庫的操作
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));
?
轉載于:https://www.cnblogs.com/salonliudong/archive/2006/12/04/582107.html
總結
以上是生活随笔為你收集整理的C#中oracle数据库的连接方法的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Play Framework介绍:主要概
- 下一篇: CentOs 中显示乱码问题