日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > C# >内容正文

C#

C#中oracle数据库的连接方法

發(fā)布時間:2023/12/20 C# 34 豆豆
生活随笔 收集整理的這篇文章主要介紹了 C#中oracle数据库的连接方法 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

一、關(guān)于數(shù)據(jù)庫的操作
1.數(shù)據(jù)庫連接
???? 有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.事務(wù)操作
?myConn.open();
???? ?OracleCommand insertComm = new OracleCommand();
??????????????? insertComm.Connection = myCnn;
??????????????? insertComm.Transaction = myCnn.BeginTransaction();
?try
???? {
??事務(wù)操作語句;
?? insertComm.Transaction.Commit();
???? }
?catch(exption ex)
???? {
??insertComm.Transaction.Rollback();
??MessageBox(ex.Message);
???? }
?finally
???? {
??myConn.close();
???? }

3.創(chuàng)建命令參數(shù)
??????? 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;
??????? }
?????? 這樣的話,當要對數(shù)據(jù)庫操作時就可以:
???????????? 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.數(shù)據(jù)集的瀏覽(例:將結(jié)果顯示在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.設(shè)置輸入只能是數(shù)字(例:現(xiàn)在往textBox1中輸入。如只能輸入字母的方法類似)
?private void textBox1_KeyPress(object sender, KeyPressEventArgs e)//屬性中的事件
??????? ?{
??????????? ????? e.Handled = !((Char.IsNumber(e.KeyChar)) || ((Keys)e.KeyChar == Keys.Back));
??????? ?}

6.Form窗口關(guān)閉時引發(fā)的事件:彈出一個確定退出的對話框
??private void form1_FormClosing(object sender, FormClosingEventArgs e)
?????? ? {
????????? if (MessageBox.Show("是否退出系統(tǒng)?", "確認", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
??????????? {
????????????????? e.Cancel = false;
??????????? }
????????? else
??????????? {
?????????????? ?? e.Cancel = true;
??????????? }
??????? }

7.OracleParameter的用法
??????? 第一步:先創(chuàng)建命令參數(shù)
??????? 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語句,并調(diào)用第一步的參數(shù)(例如::pID是個參數(shù),代表調(diào)用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
???????????????? //創(chuàng)建字節(jié)數(shù)組用于給IMAGE字段賦值,fileLength是指所選的文件的大小
???????? byte[] tmpImage = new byte[fileLength];
??????????????? //根據(jù)字節(jié)數(shù)組創(chuàng)建內(nèi)存流,之后對該流的操作將會影響字節(jié)數(shù)組的內(nèi)容
???????? MemoryStream curStream = new MemoryStream(tmpImage);
???????????????? //把控件內(nèi)顯示的圖形寫入到流中,需強制指定格式
???????? pictureBox1.Image.Save(curStream, curImageFormat);//curImageFormat前面指定的圖片格式
???????? insertComm.Parameters.Add(CreateOraParam("pPic", tmpImage));


?

轉(zhuǎn)載于:https://www.cnblogs.com/salonliudong/archive/2006/12/04/582107.html

總結(jié)

以上是生活随笔為你收集整理的C#中oracle数据库的连接方法的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。