vs2010查询oracle10g的数据

好了,上面一篇已经介绍了如何连接到数据库中,那么下面将介绍如何查询数据库中的值,仍然采用的控制台窗口的模式进行说明,根据“vs2010连接数据库”文章中代码来进行修改,修改部分用红色来表示:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Oracle.DataAccess.Client;

namespace dbtest2
{
    class Program
    {
        static void Main(string[] args)
        {
              string CONN_STR = "Data Source=(DESCRIPTION="
                + "(ADDRESS_LIST=(ADDRESS=(PROTOCOL=TCP)(HOST=192.168.128.152)(PORT=1521)))"
                + "(CONNECT_DATA=(SERVER=DEDICATED)(SERVICE_NAME=LYNN)));"
                + "User Id=fangl;Password=fangl;";

              string queryString = "SELECT ID,NAME FROM AUTO_MODEL;";

              if (string.IsNullOrEmpty(CONN_STR))//指示指定的字符串是 null 还是 Empty 字符串。
              {
                  throw new Exception("数据库访问服务出现故障,请联系晨旭公司!");
              }
              else
              {
                  OracleConnection conn = null;
                  OracleCommand command = new OracleCommand(queryString);
                  Console.WriteLine("{0}", queryString);
     

                      conn = new OracleConnection(CONN_STR);
                      command.Connection = conn;
                      conn.Open();
                      Console.WriteLine("正在连接");
                      OracleDataReader reader = command.ExecuteReader();
                      try
                      {
                          while (reader.Read())
                          {
                                       Console.WriteLine(reader.GetString(1)+",");
                          }
                    
                       }
                  catch (Exception e)
                  {
                      Console.WriteLine("{0}second exception caught",e);
                      Console.ReadLine();
                  }
                 
                  Console.WriteLine("连接成功");
                  Console.ReadLine();

                      reader.Close();
                      if (null != conn)
                      {
                          conn.Close();
                          conn.Dispose();
                      }
 
              }
        }


    }
}
 

你可能感兴趣的:(oracle,数据,职场,查询,休闲)