夢幻之旅

          DEBUG - 天道酬勤

             :: 首頁 :: 新隨筆 :: 聯(lián)系 :: 聚合  :: 管理 ::
            671 隨筆 :: 6 文章 :: 256 評論 :: 0 Trackbacks
          建立連接分為六步:
          1.定義連接字符串,oracle 的連接字符串為:
          private static string connString = "Data Source=192.168.1.130:1521/mydata;Persist Security Info=True;User ID=em_test;Password=test123;Unicode=True";
          2.根據(jù)連接字符串成連接
          3.打開連接
          4.實例命令對象
          5.操作
          6.關閉連接

          看一個例子: 
          using System;
          using System.Collections.Generic;
          using System.ComponentModel;
          using System.Data;
          using System.Drawing;
          using System.Linq;
          using System.
          Text;
          using System.Windows.Forms;
          using System.Data.OracleClient;

          namespace OpenCloseDB
          {
              
          public partial class Form1 : Form
              {
                  
          public Form1()
                  {
                      InitializeComponent();
                  }

                  private void button1_Click(object sender, EventArgs e)
                  {
                      OracleConnection connection 
          = null;
                      
          int num = 0;
                      string message 
          = "";
                      String sql 
          = "select count(*from ex_user";
                      try
                      {
                          string connString 
          = "Data Source=mydata;Persist Security Info=True;User ID=test;Password=huikk123;Unicode=True";

                          
          // 創(chuàng)建connection對象
                          connection 
          = new OracleConnection(connString);

                          
          // 打開連接
                          connection.
          Open();
                          OracleCommand command 
          = new OracleCommand(sql, connection);
                          num 
          = int.Parse(command.ExecuteScalar().ToString());
                          message 
          = string.Format("共有{0}條用戶信息!", num);
                          MessageBox.Show(message);
                      }
                      catch (Exception ex)
                      {

                          MessageBox.Show("出現(xiàn)異常"
          +ex.Message);
                      }
                      finally
                      {
                          
          if (connection != null)
                          {
                              connection.
          Close();
                          }
                      }
                  }
              }
          }
          posted on 2011-07-15 00:59 HUIKK 閱讀(488) 評論(0)  編輯  收藏 所屬分類: C#
          主站蜘蛛池模板: 乐业县| 普兰县| 沂水县| 抚宁县| 金昌市| 饶河县| 美姑县| 吐鲁番市| 大田县| 桂东县| 荣昌县| 麻栗坡县| 城固县| 象州县| 琼结县| 三门峡市| 临朐县| 凤阳县| 寻甸| 台东县| 伊通| 永善县| 卢氏县| 临清市| 上虞市| 普兰县| 全南县| 遵义市| 孝义市| 嘉禾县| 绥棱县| 嘉祥县| 陕西省| 凤山市| 灵石县| 诏安县| 池州市| 巩义市| 定日县| 绥宁县| 永善县|