Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/258.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/66.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何在Windows窗体文本框中显示MySQL数据库中的表_C#_Mysql_Database_Winforms - Fatal编程技术网

C# 如何在Windows窗体文本框中显示MySQL数据库中的表

C# 如何在Windows窗体文本框中显示MySQL数据库中的表,c#,mysql,database,winforms,C#,Mysql,Database,Winforms,我正在创建一个应用程序,当您按下按钮时,它会显示一个数据表(id、姓名、年龄、城堡)。我不知道如何让它在点击按钮后将表格显示在文本框中 以下是Windows窗体的显示: 一旦按下“从所选城堡获取人员”按钮,我希望它在文本框中显示表格 using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Li

我正在创建一个应用程序,当您按下按钮时,它会显示一个数据表(id、姓名、年龄、城堡)。我不知道如何让它在点击按钮后将表格显示在文本框中

以下是Windows窗体的显示:

一旦按下“从所选城堡获取人员”按钮,我希望它在文本框中显示表格

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.OleDb;
using System.Data.Common;

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

        private static DbConnection CreateMySQLConnection()
        {
            String connectionString = "id=csuperson;persistsecurityinfo=True;database=westeros;allowuservariables=True";

            DbProviderFactory factory = DbProviderFactories.GetFactory("MySql.Data.MySqlClient");
            DbConnection conn = factory.CreateConnection();
            conn.ConnectionString = connectionString;
            return conn;
        }

        private static void PrintPeopleByCastle(DbConnection conn, String castleValue)
        {
            DbCommand mycmd = conn.CreateCommand();
            String mySqlStmt = "select* from  person";

            if (castleValue != "all") mySqlStmt += " where castle='" + castleValue + "'";
            mycmd.CommandText = mySqlStmt;
            mycmd.CommandType = System.Data.CommandType.Text;

            DbDataReader dataReader = mycmd.ExecuteReader();
            Console.WriteLine("\n Executing: " + mySqlStmt);

            while (dataReader.Read())
            {
                Console.WriteLine(dataReader[0].ToString().PadRight(5)
                    + dataReader[1].ToString().PadRight(20)
                    + dataReader[2].ToString().PadRight(5)
                    + dataReader[3].ToString().PadRight(20));
            }
            dataReader.Close();
        }

        private static void UpdatePeople (DbConnection conn)
        {
            DbCommand mycmd = conn.CreateCommand();
            mycmd.CommandText = "update person set age = (age+1) where castle = 'WINTERFELL'";
            mycmd.CommandType = System.Data.CommandType.Text;

            int recordsAffected = mycmd.ExecuteNonQuery();
            Console.WriteLine("\nRecords affected (see age)... " + recordsAffected);
            Console.WriteLine();
        }

        private void getPeopleButton_Click(object sender, EventArgs e)
        {
          try
            {
                DbConnection conn = CreateMySQLConnection();
                conn.Open();
                PrintPeopleByCastle(conn, "all");
                conn.Close();
            }
            catch (Exception x)
            {
                Console.WriteLine("ERROR: " + x.Message);
            }
            Console.Read();
        }

        private void youngerButton_Click(object sender, EventArgs e)
        {

        }

        private void olderButton_Click(object sender, EventArgs e)
        {

        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {

        }
    }
}
我的SQL脚本是:

CREATE DATABASE westeros;

CREATE TABLE `westeros`.`person` (
 `id` INT NOT NULL,
 `name` VARCHAR(45) NULL,
 `age` VARCHAR(45) NULL,
 `castle` VARCHAR(45) NULL,
 PRIMARY KEY (`id`));

INSERT INTO `westeros`.`person` (`id`, `name`, `age`, `castle`) VALUES ('100', 'Arya
Stark', '12', 'Winterfell');
INSERT INTO `westeros`.`person` (`id`, `name`, `age`, `castle`) VALUES ('200',
'Daenerys Targaryen', '20', 'Dragonstone');
INSERT INTO `westeros`.`person` (`id`, `name`, `age`, `castle`) VALUES ('300',
'Cersei Baratheon', '35', 'KingsLanding');
INSERT INTO `westeros`.`person` (`id`, `name`, `age`, `castle`) VALUES ('400',
'Tiryon Lannister', '32', 'KingsLanding');
INSERT INTO `westeros`.`person` (`id`, `name`, `age`, `castle`) VALUES ('500', 'Jon
Snow', '21', 'Winterfell');

DbConnection如果无法打开,则应引发异常。如果需要,请检查嵌套异常以了解提供程序无法打开连接的原因。(例如,错误的凭证、找不到主机等)是否有一个gui组件,特别是您希望将其放入其中?我看不出文本框与此有多大关系(即使是多行)