使用数据库 [英] Working with a database

查看:77
本文介绍了使用数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用C#和Access数据库(最终将是sql)。我正在尝试做的是从两列读取数据。一列是标识符,另一列是我需要的数据。



我只需要来自特定行的数据。数据(测量值)自动从机器进入。我希望能够在每次进入时实时读取这些数据,然后根据测量值得到矩形变色,渐变样式。这个想法是有人可以根据其颜色查看屏幕并轻松查看其编号是否可接受。绿色,黄色和红色,强度根据测量值与其最小/最大限制的接近程度而变化。



我实际上不需要查看数据,控制颜色对象会更重要。



这是一个例子:



1.数据被读入数据库

2.我的程序被警告(或者只是设置为每60秒检查一次)

3.程序循环一个循环,根据标准检查数据< br $> b $ b

(即应该是1.0 +/-。5。实际上是.51所以矩形显示为黄色)



(即,应该是1.0 +/-。5。实际上是1.48所以矩形显示为红色)



接近名义的数字会显示绿色,它越低,它就会开始变黄。它越高,就越红。这可能适用于20种不同的尺寸,每种尺寸都有一个矩形。



这是基本的想法。



我尝试了什么:



我已经连接到数据库并找到了我需要的表和列。我已经尝试了很多东西来完成这项工作,但数据库并不是我习惯使用的东西,所以我甚至无法清楚地表达我尝试过的东西。我希望有人能指出我正确的方向。

I'm using C#, and an Access database (will eventually be sql). What I'm attempting to do is read data from two columns. One column is the identifier, and the other is the data I need.

I only need data from specific rows though. The data (measurements) comes in from a machine automatically. I'd like to be able to read this data in real time, each time it comes in, and then have a rectangle change color, gradient style, based on the measurement. The idea is that someone could look at the screen and easily see if their number is acceptable, based on its color. Green, yellow and red, intensity changes based on how close measurement is to its min/max limits.

I do not actually need to see the data, controlling colors in the object would be more important.

Here's an example:

1. Data gets read into the database
2. My program is alerted (or is just set to check every 60 seconds)
3. The program iterates through a loop, checking the data against a standard

(i.e., should be 1.0 +/-.5. Is actually .51 so a rectangle shows yellow)
or
(i.e., should be 1.0 +/-.5. Is actually 1.48 so a rectangle shows red)

A number close to nominal would show green, the lower it goes it would start to become more yellow. The higher it goes, it gets more red. There are probably 20 different dimensions this would apply to, and there would be a rectangle for each.

That's the basic idea.

What I have tried:

I have connected to the database and found the table and columns I need. I have tried many numerous things to make this work, but databases are not something I'm used to working with, so I cannot even express clearly what I've tried. I'm hoping someone can point me in the right direction.

推荐答案

也许你可以使用 OleDbDataReader ,参见这里的例子:在C#中使用Access数据库? - 堆栈溢出 [ ^ ]
Maybe you can use OleDbDataReader, see example here: Using Access databases in C#? - Stack Overflow[^]


这方面的几个部分。



第一个是从数据库中获取值。真的没有太多关于你的问题的具体细节,但从我可以告诉你,你使用MS Access。这是一个非常通用的示例,因为我对表或列名称一无所知。这样做只是从你传入的任何仪器标识符中获取值。显然你需要更改一些值和可能的类型。
A few parts to this.

The first is going to be getting the value from the database. Really not too much to work with on the specifics from your question, but from what I can tell is that you working with MS Access. This is a pretty generic sample as I know nothing about the tables or column names. What this will do is just get the value back from whatever instrument identifier you pass in. You will obviously need to change some values and possible types.
public Decimal GetInstrumentValue(int InstrumentID) {
     string AccessConnection = "ACE12 connection string";
     string AccessQuery = "SELECT Reading FROM Table WHERE Identifier = ?";
     OleDbConnection conn = new OleDbConnection(_con)) {
          OleDbCommand cmd = new OleDbCommand(AccessQuery, conn);
          cmd.Parameters.AddWithValue("@Identifier", YourIdentifierValue);
          conn.Open();
          Decimal RetrievedValue = (Decimal)cmd.ExecuterScalar();
          conn.Close();
     }
}



现在为着色部分。具有绝对阈值的简单黄绿红是简单的,如果 - 然后逻辑


Now for the coloring portion. The simple yellow-green-red with absolute thresholds is simple if-then logic

// NO specific language here, proto-code
if (value < 0.5) set color to yellow
else if (value >  1.5) set color to red
else set color to green



执行渐变颜色如果没有一百万if-then行将会更复杂。


Doing the gradual color change is going to be a little more complicated without a million if-then lines.


谢谢你。我认为看到我已经连接到数据库的方式不是这样的:



Thanks for that. I thought seeing as how I was already connected to the database that none of this:

"
string AccessConnection = "ACE12 connection string";
      string AccessQuery = "SELECT Reading FROM Table WHERE Identifier = ?";
      OleDbConnection conn = new OleDbConnection(_con)) {
      OleDbCommand cmd = new OleDbCommand(AccessQuery, conn);





本来是必要的。或者我应该忽略我当前的连接,只需用你的代码提取我需要的数据提供?这只是在测试阶段。实际的程序将使用SQL数据库。



至于其他一切,我只需要使用标准的SQL查询实现我的目标?



would have been necessary. Or should I be looking at disregarding my current connection, and just pulling the data I need with the code you provided? This is just in a testing phase. The actual program will be using an SQL database.

As for everything else, I only need to use standard SQL queries to achieve my goal?


这篇关于使用数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆