繁体   English   中英

MySql 数据库中的数据未显示在 datagridview Windows 表单中,C#

[英]Data in the MySql database not displaying in the datagridview Windows form, C#

我执行了以下代码,当我通过单击“addbtn”插入数据时,它会插入到 MYSQL 数据库中。 我可以通过 localhost/phpmyadmin 查看插入的数据。 但数据未显示在 windows 表格中。 我怎样才能解决这个问题? 第一个代码是 Dbconnection.cs

using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace dbtest
{
    class Dbstudent
    {
        public static MySqlConnection GetConnection()
        {
            string sql = "datasource=localhost;port=3306;username=root;password=;database=student";
            MySqlConnection con = new MySqlConnection(sql);
            try
            {
                con.Open();
            }
            catch(MySqlException ex )
            {
                MessageBox.Show("MySql connection! \n"+ex.Message, "Error",MessageBoxButtons.OK,MessageBoxIcon.Error);
            
            }
            return con;
        }
        public static void Addstudent(student std)
        {
            string sql = "INSERT INTO studentinfo VALUES(NULL,@Studentnic,@StudentName,@StudentAddress)";
            MySqlConnection con = GetConnection();
            MySqlCommand cmd = new MySqlCommand(sql, con);
            cmd.CommandType = CommandType.Text;
            cmd.Parameters.Add("@Studentnic", MySqlDbType.VarChar).Value = std.nic;
            cmd.Parameters.Add("@StudentName", MySqlDbType.VarChar).Value = std.name;
            cmd.Parameters.Add("@StudentAddress", MySqlDbType.VarChar).Value = std.address;

            try
            {
                cmd.ExecuteNonQuery();
                MessageBox.Show("Added Succesfully.", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);

            }
            catch(MySqlException ex)
            {
                MessageBox.Show("Student not inserted \n" + ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            con.Close();
        }
      public static void DisplayAndSearch(String query,DataGridView dgv)
          {
              string sql = query;
              MySqlConnection con = GetConnection();
              MySqlCommand cmd = new MySqlCommand(sql, con);
              MySqlDataAdapter adp = new MySqlDataAdapter(cmd);
              DataTable tbl = new DataTable();
              adp.Fill(tbl);
              dgv.DataSource = tbl;
              con.Close();
          } 

    }
}

第二个代码是 Form1.cs

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;

namespace dbtest
{
    public partial class Form1 : Form
    {
       
        public Form1()
        {
            InitializeComponent();
        }
      
        public void Display()
        {
            Dbstudent.DisplayAndSearch("SELECT ID,NIC,Name,Address FROM studentinfo",dataGridView1);

        } 

        public void Clear()
        {
            txtnic.Text = txtname.Text = txtaddress.Text = string.Empty;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if(txtnic.Text.Trim().Length<11 || txtnic.Text.Trim().Length > 11)
            {
                MessageBox.Show("Student NIC must include 11 characters");
                return;
            }
            if (txtname.Text.Trim().Length < 2 )
            {
                MessageBox.Show("Student name must not be empty");
                return;
            }
            if (txtaddress.Text.Trim().Length < 2)
            {
                MessageBox.Show("Student address must not be empty");
                return;
            }
            if(addbtn.Text=="ADD")
            {
                student std = new student(txtnic.Text.Trim(),txtname.Text.Trim(), txtaddress.Text.Trim());
                Dbstudent.Addstudent(std);
                
                Clear();
               
            }
            Display();

        }

        private void panel1_Paint(object sender, PaintEventArgs e)
        {

        }

        private void textBox2_TextChanged(object sender, EventArgs e)
        {

        }

        private void idlabel_Click(object sender, EventArgs e)
        {

        }

        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            Display();
        }
    }
}

试试这个>

DataTable dt = new DataTable("CharacterInfo");

& 然后

dgv.DataSource =tbl.DefaultView;

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM