如何在c#中从mysql读取和打印数据
发布时间:2020-12-15 08:38:46 所属栏目:百科 来源:网络整理
导读:我的问题是我无法从我的 mysql数据库中的表中打印出所有数据,我在给定的表“老师”中排在最后一行.有没有人可以帮我找到错误? using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using S
我的问题是我无法从我的
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"); } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |