C# 串連Oracle 資料庫 並將資料庫表顯示出來 代碼

來源:互聯網
上載者:User

添加引用:

using System.Data.OracleClient;

在此處我選擇了一個Oracle(服務名(Data Source 此處的Data和Source是分開的):GIS 使用者名稱(user):gis   密碼(password):gis)自己的Oracle會有自己的名字 請修改一下

我選擇了一個叫做“管線”的表  其中“MSFS,BH”皆是表中的列

上代碼:

記得加一個button按鈕

   private void button1_Click(object sender, EventArgs e)

        {

            string connection = "Data Source = GIS;user = gis; password = gis";//資料庫的串連 資料來源 使用者 密碼

            OracleConnection coon = new OracleConnection(connection);//建立資料庫連接

           

            OracleCommand cmd = new OracleCommand("select MSFS,BH from 管線  ",coon);//執行資料連線    如果想選所有的列可將MSFS,BH改為* 即 “select * from 管線”

 

            DataSet ds1;

 

            ds1 = new DataSet();//定義資料集

 

            OracleDataAdapter da1 = new OracleDataAdapter(cmd);//取出資料表

            da1.Fill(ds1);//將資料載入到資料集中

 

            DataTable dt = ds1.Tables[0];//將資料放入表中

 

            coon.Close();//關閉資料庫連接

 

            ///遍曆

            ///  //表為空白則返回

           //下面的代碼可對錶進行操作 如果想直接顯示可直接加上資料顯示代碼  下面的if  else代碼就不用了

            if (dt == null)

            {

                return;

            }

            else

            {

                for (int i = 0; i < dt.Rows.Count; i++)

                {

                  

                }

            }

            //資料顯示 在dataGridView中顯示

            this.dataGridView1.DataSource = dt.DefaultView;

            this.dataGridView1.Refresh();

        }

相關文章

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.