c# - MySql 数据库中的数据未显示在 datagridview Windows 窗体中,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;
推荐阅读
- python - 添加中断后循环没有中断
- algorithm - 快速排序中三个(平均值)的中位数?
- bert-language-model - 如何从罗伯塔变压器中获得正确的嵌入?
- swift - 在非转义函数中处理转义闭包
- hybris - 新的自定义段落组件中的 Spartacus 编译错误
- node.js - 如果nodejs中的语句没有按顺序执行
- django - Django urldispatcher:将请求定向到特殊视图
- kubernetes - 有没有办法向 Kubernates Ingress (nginx) 添加注释?
- windows - 将命令行保留为 Powershell 而不是 CMD
- github - 在 GitHub 上的 PR 操作期间,实际运行的是哪个版本的代码?