【问题标题】:How to read and print out data from mysql in c#如何在c#中从mysql中读取和打印数据
【发布时间】:2012-09-06 16:25:42
【问题描述】:

我的问题是我无法从我的 mysql 数据库中的表中打印出所有数据,我只打印了给定表“老师”中的最后一行。有没有人可以帮我找出错误?

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MySql.Data.MySqlClient;

namespace ReadDataFromMysql
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            string sql = " SELECT * FROM teacher  ";
            MySqlConnection con = new MySqlConnection("host=localhost;user=root;password=859694;database=projekt;");
            MySqlCommand cmd = new MySqlCommand(sql, con);

            con.Open();

           MySqlDataReader  reader = cmd.ExecuteReader();

           while (reader.Read()) {
               data2txt.Text = reader.GetString("id");
              datatxt.Text = reader.GetString("userId");
           }

        }

        private void btnclose_Click(object sender, EventArgs e)
        {
            Close();
        }
    }
}

【问题讨论】:

  • 你在while循环中放了断点吗?您的文本字段将由查询返回的所有值填充,并将在 while 循环返回时设置为最后一个值。

标签: c# mysql


【解决方案1】:

此代码有效。

private void getdata()
{
MySqlConnection connect = new MySqlConnection("SERVER=localhost; user id=root; password=; database=databasename");
MySqlCommand cmd = new MySqlCommand("SELECT ID, name FROM data WHERE ID='" + txtid.Text + "'");
cmd.CommandType = CommandType.Text;
cmd.Connection = connect;
connect.Open();
try
{
MySqlDataReader dr;
dr = cmd.ExecuteReader();
while(dr.Read())
{
txtID.Text = dr.GetString("ID");
txtname.Text = dr.GetString("name");
}
dr.Close();
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{
if(connect.State == ConnectionState.Open)
{
connect.Close();
}
}

【讨论】:

    【解决方案2】:

    您的问题是您在每行数据上覆盖 data2txt.Text 和 datatxt.Text 。如果您想查看这些字段中的所有数据,这样的事情应该可以满足您的需要:

    data2txt.Text = string.Empty;
    datatxt.Text = string.Empty;
    
    while (reader.Read())
    {
        data2txt.Text += $"{reader.GetString("id")};";
        datatxt.Text += $"{reader.GetString("userId")};";
    }
    

    【讨论】:

      【解决方案3】:

      你应该在再次写入之前输出数据:

      data2txt.Text = reader.GetString("id");
                datatxt.Text = reader.GetString("userId");
      

      或者使用 var 将所有数据存储在每个“读取”中,然后输出该 var

      varexample.Text += reader.GetString("id");
      

      【讨论】:

        【解决方案4】:

        您分配的是每个字段的值,而不是现有控件文本的值加上新值。添加一个断点以确保您获得多行,但在编写代码时,您只会在表单中看到一行的结果,因为您在循环中的每次迭代中都会覆盖。

        【讨论】:

          【解决方案5】:

          显然,您的代码将教师表的最后一行值显示到表单上的文本字段中。因为您正在循环通过数据读取器并将值分配给 textfiled。因此每次迭代都会覆盖文本框中的先前值。

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2011-01-28
            • 2017-12-30
            • 1970-01-01
            相关资源
            最近更新 更多