c# - 从 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);
}
}
}
解决方案
您需要调用 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();
}
推荐阅读
- html - 为什么这个边距在这里?
- google-apps-script - 复制到新工作表乘以自己的负载,并过滤我的结果
- css - 如何删除两个 div 之间的盒子阴影,使其像在同一平面上一样?
- node.js - 如何在事务中插入多行(来自请求正文)
- flutter - 从 Hive 框中删除的对象仍会加载到内存中
- c++ - 无法通过 cv::imshow() 显示窗口。我错过了什么?
- git - 如何只推送一个更改的文件
- javascript - 问题 nuxt.js Vue.js 与图像滑块更改照片第一通道第二张照片不显示 backslide1 未添加到 dom
- c# - 为什么序列化方法需要实例才能使用?
- r - 选择最接近定义值的元素,同时避免重复