zoukankan      html  css  js  c++  java
  • C#操作数据库

    用DataSet方法读取数据

    OracleHelper.cs

     1 using System;
    2 using System.Collections.Generic;
    3 using System.Linq;
    4 using System.Text;
    5 using System.Data;
    6 using System.Data.OracleClient;
    7
    8 namespace TestOracleOperation
    9 {
    10 class OracleHelper
    11 {
    12 private OracleConnection connection = null;
    13
    14 private void OpenDB(string connStr)
    15 {
    16 connection = new OracleConnection(connStr);
    17
    18 if (connection.State == ConnectionState.Closed)
    19 {
    20 connection.Open();
    21 }
    22 }
    23
    24 private void CloseDB()
    25 {
    26 if (connection != null && connection.State == ConnectionState.Open)
    27 {
    28 connection.Close();
    29 connection.Dispose();
    30 }
    31
    32 }
    33
    34 /// <summary>
    35 /// 查询数据库
    36 /// </summary>
    37 /// <param name="sqlStr">查询语句</param>
    38 /// <param name="connStr">连接数据库</param>
    39 /// <param name="tableName">存放DataTable对象的</param>
    40 /// <returns></returns>
    41 public DataSet Query(string sqlStr, string connStr, string tableName)
    42 {
    43 DataSet dataSet = new DataSet();
    44
    45 try
    46 {
    47 OpenDB(connStr);
    48
    49 OracleCommand command = new OracleCommand(sqlStr, connection);
    50
    51 OracleDataAdapter dataAdapter = new OracleDataAdapter(command);
    52
    53 dataAdapter.Fill(dataSet, tableName);
    54 }
    55 finally
    56 {
    57 CloseDB();
    58 Console.WriteLine(connection.State);
    59 }
    60
    61 return dataSet;
    62 }
    63 }
    64 }

    Program.cs

     1 using System;
    2 using System.Data;
    3
    4 namespace TestOracleOperation
    5 {
    6 class Program
    7 {
    8 static void Main(string[] args)
    9 {
    10 string conStr = "Data Source=testOracle;User Id=test;Password=test;";
    11 string sqlComm = "SELECT * FROM users where rownum<10";
    12
    13 OracleHelper oracleHelper = new OracleHelper();
    14
    15 DataSet accountSet = oracleHelper.Query(sqlComm, conStr, "users ");
    16
    17 foreach (DataRow row in accountSet.Tables["users "].Rows)
    18 {
    19 Console.WriteLine(row["user_id"] + "\t" + row["user_passport"]);
    20 }
    21
    22 Console.ReadKey();
    23
    24 }
    25 }
    26 }

    参考:http://www.cnblogs.com/dongdonghuihui/archive/2009/08/13/1545446.html


  • 相关阅读:
    智能指针unique_ptr记录
    ubuntu系统火狐无法播放网页视频
    javascript中json对象json数组json字符串互转及取值
    C#压缩文件
    C#异步编程
    C# POST请求 json格式
    C# Http方式下载文件到本地类
    C#中NPOI操作excel之读取和写入excel数据
    浅析C#中抽象类和接口的区别
    C#自动实现Dll(OCX)控件注册的两种方法
  • 原文地址:https://www.cnblogs.com/Amei595409929/p/2393949.html
Copyright © 2011-2022 走看看