C#中oracle資料庫的串連方法

來源:互聯網
上載者:User

一、關於資料庫的操作
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));

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.