夢幻之旅

          DEBUG - 天道酬勤

             :: 首頁 :: 新隨筆 :: 聯系 :: 聚合  :: 管理 ::
            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.根據連接字符串成連接
          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";

                          
          // 創建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("出現異常"
          +ex.Message);
                      }
                      finally
                      {
                          
          if (connection != null)
                          {
                              connection.
          Close();
                          }
                      }
                  }
              }
          }
          posted on 2011-07-15 00:59 HUIKK 閱讀(488) 評論(0)  編輯  收藏 所屬分類: C#
          主站蜘蛛池模板: 广州市| 濮阳县| 宜兰县| 南陵县| 萨嘎县| 新建县| 蓝田县| 托克逊县| 南康市| 阜平县| 清徐县| 桐乡市| 淄博市| 旌德县| 繁峙县| 通州区| 工布江达县| 三明市| 泰宁县| 庄河市| 肃北| 岳池县| 洪江市| 新昌县| 贺兰县| 安丘市| 镇坪县| 南康市| 刚察县| 吕梁市| 汝阳县| 中卫市| 南开区| 太和县| 衡阳市| 元阳县| 商洛市| 礼泉县| 天柱县| 南投县| 彰化县|