C# - 从MySQL中检索数据并在“页面”中排序它们没有DataGridView [英] C# -- Retrieving data from MySQL and ordering them in "pages" without DataGridView

查看:140
本文介绍了C# - 从MySQL中检索数据并在“页面”中排序它们没有DataGridView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是C#编程中的新手,我正在开始创建一个应用程序,通过ProgressBars到达现实生活中的事件。
所有信息。关于这些事件存储在数据库中;所以打开一个连接到my-sql数据库我可以检索所有我的事件。
问题是我不知道该数据库中存储了多少个事件(因为在一天中我可以有20个事件,另一个可以有其他40个事件),并且我的Form1无法根据事件数量。
所以,我需要制作一个页面系统,以限制每个页面中打印的事件。
在Google和本网站上搜索,我看到在C#中创建页面的最简单方法是使用DataGridView,但是由于需要ProgressBars才能查看事件何时到期,因此我无法使用它。
所以,我想使用MySQL的限制数据选择来解决这个问题: SELECT * from EVENTS LIMIT x,y; 其中x和y是两个变量,他们按几个按钮改变;在按钮1(第一页)上,x和y将为0和20,在按钮2(第二页)上,x和y将为20,40,依此类推。
但是这不起作用,因为x和y每次刷新Form1时都会重新设置。



这只是一段代码,以帮助您了解我的场景:

  public partial class Form1:Form 
{
int x;
private void Form1_Load(object sender,EventArgs e)
{
server =localhost;
database =app;
uid =root;
password =root;
string connectionString;
connectionString =SERVER =+ server +; +DATABASE =+ database +; +UID =+ uid +; +PASSWORD =+密码+;;
connection = new MySqlConnection(connectionString);
if(this.OpenConnection()== true)
{
string query =SELECT * from EVENTS LIMIT+ x +,+ y +;;
mySqlDataAdapter = new MySqlDataAdapter(query,connection);
...
...
}
}
private void button1_Click(object sender,EventArgs e)
{
x = 0;
y = 20;
}
private void button2_Click(object sender,EventArgs e)
{
x = 20;
y = 40;
}
}

所以,我期待着任何想法;

解决方案

在您的按钮中点击您更新x和y值,但是您永远不会重新执行数据库查询或更新数据在表格上。您需要提取数据的下一个页面才能显示。



首先,将数据库代码提取为方法而不是加载事件:

  private void LoadData()
{
server =localhost;
database =app;
uid =root;
password =root;
string connectionString;
connectionString =SERVER =+ server +; +DATABASE =+ database +; +UID =+ uid +; +PASSWORD =+密码+;;
connection = new MySqlConnection(connectionString);
if(this.OpenConnection()== true)
{
string query =SELECT * from EVENTS LIMIT+ x +,+ y +;;
mySqlDataAdapter = new MySqlDataAdapter(query,connection);
...
...
}
}

然后在 Load 事件中设置您的初始 x y 值并加载数据:

  private void Form1_Load(object sender,EventArgs e)
{
x = 0;
y = 20;
LoadData();
}

这应该加载数据的初始页面。那么大概你有一个下一个和上一个按钮进行分页?每个都会相应地增加/减少 x y 值。这样的东西:

  private void nextButton_Click(object sender,EventArgs e)
{
x + = 20 ;
y + = 20;
LoadData();
}

private void previousButton_Click(object sender,EventArgs e)
{
x - = 20;
y - = 20;
LoadData();
}

进一步,您需要添加一些边界检查,以便人们可以在第一页上点击最后一页或上一页。可能有一些错误处理是一个好主意。有很多方法来抛光这个。但基本思想是您需要重新查询数据库,并重新绑定每个页面的表单控件。


I'm newbie in C# programmation and I'm starting creating an application that show me when (real life) "events" expire through ProgressBars. All info. about these events are stored in a Database; So opening a connection to my-sql database I can retrieve all my events. The problem is that I don't know how many events are stored in that database (since in a day I can have 20 events and in another I could have other 40 ones) and my Form1 can't resize itself according the number of events. So, I need to make a page system in order to limit the events printed in each page. Searching on Google and also on this site, I see that the easiest way to make pages in C# is using DataGridView but I can't use it because I need ProgressBars in order to see when an event expires. So, I thought to risolve this problem using the Limit data selection of MySQL: SELECT * from EVENTS LIMIT x,y;where x and y are two variables and they change according several buttons; on button 1 (first page) x and y would be 0 and 20, on button 2 (second page) x and y would be 20,40 and so on.. but this doesn't work since x and y became resetted each time I refresh the Form1.

This is just a piece of code in order to help you to understand my scenario:

public partial class Form1 : Form
{
    int x;
private void Form1_Load(object sender, EventArgs e)
    {
        server = "localhost";
        database = "app";
        uid = "root";
        password = "root";
        string connectionString;
        connectionString = "SERVER=" + server + ";" + "DATABASE=" + database + ";" + "UID=" + uid + ";" + "PASSWORD=" + password + ";";
        connection = new MySqlConnection(connectionString);
        if (this.OpenConnection() == true)
        {
            string query = "SELECT * from EVENTS LIMIT"+ x +","+ y +";";
            mySqlDataAdapter = new MySqlDataAdapter(query , connection);
            ...
            ...
        }
    }
private void button1_Click(object sender, EventArgs e)
    {
        x=0;
        y=20;
    }
private void button2_Click(object sender, EventArgs e)
    {
        x=20;
        y=40;
    }
 }

So, I'm looking forward for any idea;

解决方案

In your button clicks you update the x and y values, but you never re-execute the database query or update the data on the form. You need to fetch the next "page" of data in order to display it.

First, extract your database code into a method rather than in the Load event:

private void LoadData()
{
    server = "localhost";
    database = "app";
    uid = "root";
    password = "root";
    string connectionString;
    connectionString = "SERVER=" + server + ";" + "DATABASE=" + database + ";" + "UID=" + uid + ";" + "PASSWORD=" + password + ";";
    connection = new MySqlConnection(connectionString);
    if (this.OpenConnection() == true)
    {
        string query = "SELECT * from EVENTS LIMIT"+ x +","+ y +";";
        mySqlDataAdapter = new MySqlDataAdapter(query , connection);
        ...
        ...
    }
}

Then in the Load event set your initial x and y values and load the data:

private void Form1_Load(object sender, EventArgs e)
{
    x = 0;
    y = 20;
    LoadData();
}

This should load the initial "page" of data. Then presumably you have a "next" and "previous" button for paging? Each one would increment/decrement the x and y values accordingly. Something like this:

private void nextButton_Click(object sender, EventArgs e)
{
    x += 20;
    y += 20;
    LoadData();
}

private void previousButton_Click(object sender, EventArgs e)
{
    x -= 20;
    y -= 20;
    LoadData();
}

Going further, you'll want to add some bounds checking so that people can't click "next" on the last page or "previous" on the first page. Probably some error handling would be a good idea. There are lots of ways to polish this. But the basic idea is that you need to re-query the database and re-bind the form controls with each page.

这篇关于C# - 从MySQL中检索数据并在“页面”中排序它们没有DataGridView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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