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

时间:2015-12-10 02:43:51

标签: c# mysql database winforms

我正在创建一个应用程序,当您按下按钮时,该应用程序会显示数据表(id,name,age,castle)。我不确定如何在单击按钮后将表格显示在文本框中。

以下是Windows窗体的显示:http://puu.sh/lPSi6/204b0d15c0.png

按下“从所选城堡中获取人物”按钮后,我希望它将表格显示在文本框中。

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');

http://puu.sh/lPSCc/6034433a44.png

0 个答案:

没有答案