WinForm里面连接Oracle数据库

           string oradb = "Data Source=(DESCRIPTION="
             + "(ADDRESS=(PROTOCOL=TCP)(HOST=MyComputerName)(PORT=1521))"
             + "(CONNECT_DATA=(SERVICE_NAME=DemoDB)));"
             + "User Id=SYSTEM;Password=************;";
            
            try
            {
                OracleConnection conn = new OracleConnection(oradb);
                conn.Open();
               
                string sql = " select * from DemoOP.T_TEST "; // DemoOP是表T_TEST的user
                OracleCommand cmd = new OracleCommand(sql, conn);
                cmd.CommandType = CommandType.Text;
              
                //大容量数据,可以进行修改,使用DataSet和DataAdapter
                DataSet ds = new DataSet();
                OracleDataAdapter da = new OracleDataAdapter();
                da.SelectCommand = cmd;
                da.Fill(ds);
                dataGridView1.DataSource = ds.Tables[0] ;

                //使用DataReader,读取数据
                OracleDataReader dr = cmd.ExecuteReader();
                while (dr.Read()) // C#
                {
                    comboBox1.Items.Add(dr["NAME"].ToString());
                }

       dr.Close();

                conn.Dispose(); //Close()也可以。
                   
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message.ToString());

            }
            finally
            {

            }

 

前面要加入using Oracle.DataAccess.Client;

posted @ 2012-07-19 22:47  ajeeky  阅读(4157)  评论(0编辑  收藏  举报