如何使用datagridview显示数据库中的值 [英] How to use datagridview to display values from a database
问题描述
我正在通过如何使用C#连接到MySQL来学习本教程 [ ^ ]
出于某种原因,我得到名称
I am following the tutorial by How to Connect to MySQL Using C#[^]
For some reason I am getting the name
dataGridView1 does not exists in the current context at the line
dataGridView1.DataSource = ds.Tables[0].DefaultView;
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 MySql.Data.MySqlClient;
namespace connect4
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
//string MyConnectionString = "server=localhost;database=testdb;uid=root;pwd=;" ;
}
private void button1_Click(object sender, EventArgs e)
{
string MyConnectionString = "server=localhost;database=testdb;uid=root;pwd=;" ;
//int MobileNo;
//string Mobile = Mobile.Text;
//int.TryParse(Mobile, out MobileNo);
MySqlConnection con = new MySqlConnection(MyConnectionString);
MySqlCommand cmd;
con.Open();
MessageBox.Show("connection open");
try {
cmd = con.CreateCommand();
cmd.CommandText = "INSERT INTO phonebook(Id, Name, MobileNo) VALUES (@ID, @Name,@MobileNo)";
cmd.Parameters.AddWithValue("@Id", int.Parse(Id.Text));
cmd.Parameters.AddWithValue("@Name", txtName.Text);
cmd.Parameters.AddWithValue("@MobileNo", int.Parse(Mobile.Text));
cmd.ExecuteNonQuery();
MessageBox.Show("Values inserted");
}
catch (Exception)
{
throw;
//MessageBox.Show("query failed");
}
finally
{
if (con.State == ConnectionState.Open)
{
con.Close();
MessageBox.Show("connection closed");
LoadData();
}
}
}
private void textBox2_TextChanged(object sender, EventArgs e)
{
}
private void LoadData()
{
string MyConnectionString = "server=localhost;database=testdb;uid=root;pwd=;";
MySqlConnection con = new MySqlConnection(MyConnectionString);
con.Open();
MessageBox.Show("connection open");
try
{
MySqlCommand cmd = con.CreateCommand();
cmd.CommandText = "SELECT * FROM phonebook";
MySqlDataAdapter adap = new MySqlDataAdapter(cmd);
DataSet ds = new DataSet();
adap.Fill(ds);
//bindingSource1.DataSource = ds;
dataGridView1.DataSource = ds.Tables[0].DefaultView;
}
catch (Exception)
{
throw;
}
finally {
if (con.State == ConnectionState.Open)
{
con.Close();
}
}
}
}
}
我尝试过:
* ----------------------------------------- ------------------------------------------------- * -------------------------------------------------- -------------------------------------------------- -------------------------------------------------- -------------------------
What I have tried:
*------------------------------------------------------------------------------------------*-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
推荐答案
ID和Mobile不以txt为前缀(大概是表格上控件的名称。
你应该做的是:
在管理工具中打开你的数据库( SSMS用于sql server,不知道是什么用于MySql)
检查记录是否已正确保存
在Button_Click中放置一个断点事件并逐步完成方法,检查val输入的用户。
ID and Mobile are not prefixed with txt (presumably the name of the control on the form.
What you should have done is:
open your database in the management tool (SSMS for sql server, not sure what for MySql)
Check that the record has been save correctly
Put a break point in the Button_Click event and stepped through the method, checking the values of the input.
这篇关于如何使用datagridview显示数据库中的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!