C#连接Oracle

yyyyyn 贡献于2013-01-28

作者 li  创建于2008-12-17 01:47:00   修改者gw  修改于2010-04-07 09:05:00字数5210

文档摘要:C#中oracle数据库的连接方法一、关于数据库的操作1.数据库连接    有2种:第一种:古老的方法(较为死板,不利于灵活操作),即用OracleConnection的类来连接。
关键词:

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)); c# 连接数据库Access,SQL Server,Oracle,MySQL,DB2,SyBase 2010-01-10 12:00:00   1.C#连接连接Access 添加引用 using System.Data; using System.Data.OleDb string strConnection="Provider=Microsoft.Jet.OleDb.4.0;"; strConnection+=@"Data Source=C:\\www\\www.35hunan.com.mdb"; OleDbConnection objConnection=new OleDbConnection(strConnection); objConnection.Open(); objConnection.Close(); 2.C#连接SQL Server 添加引用 using System.Data; using System.Data.SqlClient; string strConnection="user id=sa;password=;"; strConnection+="initial catalog=Northwind;Server=YourSQLServer;"; strConnection+="Connect Timeout=30"; SqlConnection objConnection=new SqlConnection(strConnection); objConnection.Open(); objConnection.Close(); 3.C#连接Oracle 添加引用 using System.Data.OracleClient; using System.Data; string ConnectionString="Data Source=sky;user=system;password=manager;";//写连接串 OracleConnection conn=new OracleConnection(ConnectionString);//创建一个新连接 try { conn.Open(); OracleCommand cmd=conn.CreateCommand(); cmd.CommandText="select * from MyTable";//在这儿写sql语句 OracleDataReader odr=cmd.ExecuteReader();//创建一个OracleDateReader对象 while(odr.Read())// 读取数据,如果odr.Read()返回为false的话,就说明到记录集的尾部了               { Response.Write(odr.GetOracleString(1).ToString());//输出字段1,这个数是字段索引,具体怎么使用字段名还有待研究 } odr.Close(); } catch(Exception ee) { Response.Write(ee.Message); //如果有错误,输出错误信息 } finally { conn.Close(); //关闭连接 } 4.C#连接MySQL 添加程序集 using MySQLDriverCS; // 建立数据库连接 MySQLConnection DBConn; DBConn = new MySQLConnection(new MySQLConnectionString("localhost","mysql","root","",3306).AsString); DBConn.Open(); // 执行查询语句 MySQLCommand DBComm; DBComm = new MySQLCommand("select Host,User from user",DBConn); // 读取数据 MySQLDataReader DBReader = DBComm.ExecuteReaderEx(); // 显示数据 try { while (DBReader.Read()) { Console.WriteLine("Host = {0} and User = {1}", DBReader.GetString(0),DBReader.GetString(1)); } } finally { DBReader.Close(); DBConn.Close(); } //关闭数据库连接 DBConn.Close(); 5.C#连接IBM DB2 程序代码: OleDbConnection1.Open(); OleDbDataAdapter1.Fill(dataSet1,"Address"); DataGrid1.DataBind(); OleDbConnection1.Close(); 6.C#连接SyBase Provider=Sybase.ASEOLEDBProvider.2;Initial Catalog=数据库名;User ID=用户名;Data Source=数据源;Extended Properties="";Server Name=ip地址;Network Protocol=Winsock;Server Port Address=5000;

下载文档到电脑,查找使用更方便

文档的实际排版效果,会与网站的显示效果略有不同!!

需要 4 金币 [ 分享文档获得金币 ] 0 人已下载

下载文档