關於用c#怎樣串連oracle的問題

來源:互聯網
上載者:User
嘗試連上Oracle,並用OracleCommand命令讀取資料庫中的記錄,顯示在控制台上: 1using System;
 2using System.Data;
 3using System.Data.OracleClient;
 4
 5public class MainClass
 6{
 7    static void Main()
 8    {
 9        OracleConnection conn = new OracleConnection();
10        conn.StateChange += new StateChangeEventHandler(OnStateChanged);
11        conn.ConnectionString = "Data Source = chenchong; User Id = system; Password = ;";
12        try
13        {
14            conn.Open();
15            Console.WriteLine("開啟資料庫連接成功。");
16            OracleCommand command = new OracleCommand();
17//            command.CommandText = "";
18            command.CommandText = "select nsrmc from scott.nsr where nsrmc like :pNsrmc order by nsrmc";
19            command.Connection = conn;
20            OracleParameter parameter = command.Parameters.Add("pNsrmc", OracleType.VarChar);
21            parameter.Value = "_國%";
22            OracleDataReader reader = command.ExecuteReader();
23
24            // 以下驗證是否可以同時開啟兩個DataReader
25            OracleCommand command2 = new OracleCommand();
26            command2.CommandText = "select Count(*) from scott.nsr";
27            command2.Connection = conn;
28            OracleDataReader reader2 = command2.ExecuteReader();
29            reader2.Read();
30            Console.WriteLine("資料庫共有 {0} 個記錄。", reader2.GetInt32(0));
31            reader2.Close();
32
33            while (reader.Read())
34            {
35                if (!reader.IsDBNull(reader.GetOrdinal("Nsrmc")))
36                {
37                    Console.WriteLine(reader.GetString(reader.GetOrdinal("Nsrmc")).Trim());
38                }
39//                string s = null;
40//                Console.WriteLine(s);
41            }
42            command.CommandText = "select count(nsrmc) from scott.nsr where nsrmc like :pNsrmc order by nsrmc";
43            Console.WriteLine("合格共有 {0} 個記錄。",command.ExecuteScalar());
44        }
45        catch (Exception e)
46        {
47            Console.WriteLine("出現了異常:{0}", e.ToString());
48        }
49        finally
50        {
51            if (conn != null)
52            {
53                conn.Close();
54            }
55        }
56
57        Console.ReadLine();
58    }
59
60    private static void OnStateChanged(object sender, StateChangeEventArgs e)
61    {
62        Console.WriteLine("資料庫連接的狀態發生改變,原狀態為:{0},目前狀態為:{1}。", e.OriginalState, e.CurrentState);
63    }
64}
相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.