微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

在Windows Service中从Oracle数据库访问数据

如何解决在Windows Service中从Oracle数据库访问数据

im试图创建一个Windows服务,该服务每小时连接一次oracle db,并提取一些记录并将其写入日志文件。我创建的服务运行,并按一定间隔写入文件,但似乎忽略了从数据库提取的数据。 例如,所有日志文件保存的是- 服务时间为8/24/2020 1:02:20 AM 在8/24/2020 1:02:45 AM召回服务 在8/24/2020 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);
                }
            }
        }

解决方法

您需要调用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();
}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。