如何在c#中从mysql读取和打印数据

如何在c#中从mysql读取和打印数据,第1张

概述我的问题是我无法从我的 mysql数据库中的表中打印出所有数据,我在给定的表“老师”中排在最后一行.有没有人可以帮我找到错误? using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Linq; 我的问题是我无法从我的 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();        }    }}
解决方法 您的问题是您在每行数据上覆盖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");}
总结

以上是内存溢出为你收集整理的如何在c#中从mysql读取和打印数据全部内容,希望文章能够帮你解决如何在c#中从mysql读取和打印数据所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/langs/1251012.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-07
下一篇 2022-06-07

发表评论

登录后才能评论

评论列表(0条)

保存