首页 > 解决方案 > 从 Windows 服务中的 oracle db 访问数据

问题描述

我正在尝试创建一个 Windows 服务,该服务将每小时连接一次 Oracle 数据库并提取一些记录并将其写入日志文件。我创建的服务运行,每隔一段时间写入文件,但似乎忽略了我从数据库中提取的数据。保存的所有日志文件都是这样的示例 - 服务于 2020 年 8 月 24 日上午 1:02:20 开始服务于 2020 年 8 月 24 日上午 1:02:45 召回服务于 2020 年 8 月 24 日召回 1 :03:10 AM 服务于 2020 年 8 月 24 日凌晨 1:03:21 停止

以及为什么服务不读取 oracle 数据的想法?

namespace WindowsService2
{
    public partial class Service1 : ServiceBase
    {
        Timer timer = new Timer(); // name space(using System.Timers;)  

        public Service1()
        {
            InitializeComponent();
        }

        protected override void OnStart(string[] args)
        {
            WriteToFile("Service is started at " + DateTime.Now);
            timer.Elapsed += new ElapsedEventHandler(OnElapsedTime);
            timer.Interval = 25000; //number in milisecinds  
            timer.Enabled = true;

            
        }

        protected override void OnStop()
        {
            WriteToFile("Service is stopped at " + DateTime.Now);
        }

        private void OnElapsedTime(object source, ElapsedEventArgs e)
        {
            WriteToFile("Service is recalled at " + DateTime.Now);
            //create connection to oracle dB and connect to oracle dB without SQL*NET config file AKA TSANAMES.ORA
            string conString = "user id =user; password =1234;" + "data source = storage.database.com";
          
            OracleConnection con = new OracleConnection();

            con.ConnectionString = conString;
            con.Open();
            //create command within context of the connection and pull a record from the dB
            OracleCommand pullRecord = con.CreateCommand();
            pullRecord.CommandText = "select * from person where user_id='20'";
            // execute command and use reader to display data from table
            OracleDataReader reader = pullRecord.ExecuteReader();
            string str = reader[0].ToString();
            WriteToFile("user name " + str);
            con.Close();
            
        }

        public void WriteToFile(string Message)
        {
            string path = AppDomain.CurrentDomain.BaseDirectory + "\\Logs";
            if (!Directory.Exists(path))
            {
                Directory.CreateDirectory(path);
            }

            string filepath = AppDomain.CurrentDomain.BaseDirectory + "\\Logs\\ServiceLog_" + DateTime.Now.Date.ToShortDateString().Replace('/', '_') + ".txt";
            if (!File.Exists(filepath))
            {
                // Create a file to write to.   
                using (StreamWriter sw = File.CreateText(filepath))
                {
                    sw.WriteLine(Message);
                }
            }
            else
            {
                using (StreamWriter sw = File.AppendText(filepath))
                {
                    sw.WriteLine(Message);
                }
            }
        }

标签: c#windowsoracle

解决方案


您需要调用 DataReader 的 reader.Read() 方法

private void OnElapsedTime(object source, ElapsedEventArgs e)
{
     WriteToFile("Service is recalled at " + DateTime.Now);
     //create connection to oracle dB and connect to oracle dB without SQL*NET config file AKA TSANAMES.ORA
     string conString = "user id =user; password =1234;" + "data source = storage.database.com";
      
     OracleConnection con = new OracleConnection();

     con.ConnectionString = conString;
     con.Open();
     //create command within context of the connection and pull a record from the dB
     OracleCommand pullRecord = con.CreateCommand();
     pullRecord.CommandText = "select * from person where user_id='20'";
     // execute command and use reader to display data from table
     OracleDataReader reader = pullRecord.ExecuteReader();
     if(reader.HasRows)
     {
         if(reader.Read())
         {
             //Make sure data is not null . Otherwise your service will break.
             string str = reader.GetString(0);
             WriteToFile("user name " + str);
        }
     }
     con.Close();
}

推荐阅读