我如何从数据库中选择数据以显示在gridview中 [英] how can i select data from a database to display in a gridview

查看:100
本文介绍了我如何从数据库中选择数据以显示在gridview中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好吧,我是编码的新手,正在尝试弄清楚如何在Visual Studio C#中进行编码.我有一个数据库表,该表具有3列和10行.列标题为状态","ID"和"EmployeeName".在状态"列中,每行从#1到#4都有一位数字.我想做的是利用计时器每60秒检查一次此表,并返回状态ID大于1的任何行和列.这是我到目前为止的内容:

Ok I''m new to coding and am trying to figure out how I can code in Visual Studio C#. I have a database table that has 3 columns and 10 rows. The column headings are Status, ID, and EmployeeName. In the Status column, each row has a single digit from #1 to #4. What I want to do is utilize a timer to check this table every 60 seconds and return any row and column where the status ID is greater than 1. Here is what I have so far:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.OleDb;
using System.Data;
using System.Data.SqlClient;
using System.Timers;
using System.ComponentModel;

public partial class Poll : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
    }
    public Poll()
    {
        InitializeComponent();
        timer.Interval = 60000;
        timer.Elapsed += new System.Timers.ElapsedEventHandler(timer_Elapsed);
    }
    private void InitializeComponent()
    {
    }
    System.Timers.Timer timer = new System.Timers.Timer();
    DataTable dt = new DataTable();
    void timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
    {
        using (OleDbConnection con = new OleDbConnection("MAS2000"))
        {
            OleDbDataAdapter adapter = new OleDbDataAdapter("Insert * from SensorEvent", con);
            adapter.Fill(dt);
            DataRow[] rows = dt.Select("StatusID > ''1''");
            DataTable temp = new DataTable();
            temp.Columns.Add("StatusId", typeof(int));
            temp.Columns.Add("SensorID", typeof(String));
            temp.Columns.Add("UserID", typeof(String));
            //add columns to temp
            foreach (DataRow r in rows)
            {
                //add rows to temp as follows
                temp.Rows.Add(int.Parse(r[0].ToString()), r[1].ToString(), r[2].ToString());
            }
            this.dataGrid.DataSource = temp;



它显示了表格,但是如何仅导入所需结果(状态ID大于1)的功能不起作用.我得到整张桌子.因此,对此的任何帮助将不胜感激.



It brings up the tables but the function of how to only import the desired results(status id that is higher than 1) does not work. I get the whole table. So any help with this would be very much appreciated. Thanks

推荐答案

您应该阅读有关SQL的书或至少一些在线文章.我建议仅编写查询以了解它们如何工作以及如何询问您的需求.您需要所有数据,所以就可以获取它.如果您将SQL更改为包含IF语句(例如,从sensorevent中状态为> 1的select *),则会询问您要的内容,然后您将得到它.
You should read a book, or at least some online articles, on SQL. I recommend just writing queries to learn how they work and how to ask for what you are after. You''re asking for all the data, so you''re getting it. If you change your SQL to contain an IF statement, like select * from sensorevent where status >1, then you''ll be asking for what you want, and you will then get it.


这篇关于我如何从数据库中选择数据以显示在gridview中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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