// An highlighted block
var foo = 'bar';
```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 MySQL
{
public partial class Form1 : Form
{
string connectStr = "server=127.0.0.1;port=3306;user=root;password=123456;database=chen;";
public object mda { get; private set; }
public Form1()
{
InitializeComponent();
Adapt(); //使控件宽度自适应
ConnectView(); //连接数据库操作
}
public void Adapt()
{
int width = 0;
for (int i = 0; i < this.dataGridView1.Columns.Count; i++)
{
this.dataGridView1.AutoResizeColumn(i, DataGridViewAutoSizeColumnMode.AllCells);
width += this.dataGridView1.Columns[i].Width;
}
if (width > this.dataGridView1.Size.Width)
{
this.dataGridView1.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.DisplayedCells;
}
else
{
this.dataGridView1.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
}
}
public void ConnectView()
{
MySqlConnection conn = new MySqlConnection(connectStr); //新建连接
try
{
conn.Open();
MessageBox.Show("已经建立连接");
string sqlCmd = "select * from inclinometer";
MySqlCommand cmd = new MySqlCommand(sqlCmd, conn);
MySqlDataReader reader = cmd.ExecuteReader();
BindingSource bs = new BindingSource();
bs.DataSource = reader;
this.dataGridView1.DataSource = bs;
}
catch (MySqlException ex)
{
MessageBox.Show(ex.Message);
}
finally
{
conn.Close();
}
}
private void Form1_Load(object sender, EventArgs e)
{
// TODO: 这行代码将数据加载到表“chenDataSet.course”中。您可以根据需要移动或删除它。
this.courseTableAdapter.Fill(this.chenDataSet.course);
}
private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
string M_str_sqlcon = "server=localhost;user id=root;password=123456;database=mysql2022"; //创建数据库连接对象
conn = new MySqlConnection(M_str_sqlcon);
try
{
//打开数据库连接
conn.Open();
MessageBox.Show("数据库连接成功!");
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
private void button2_Click(object sender, EventArgs e)
{
string sql = "select * from user";
mda = new MySqlDataAdapter(sql, conn);
ds = new DataSet();
mda.Fill(ds, "user");
//显示数据
dataGridView1.DataSource = ds.Tables["user"];
conn.Close();
}
private void button3_Click(object sender, EventArgs e)
{
if (mda == null || ds == null)
{
MessageBox.Show("请先导入数据!");
return;
}
try
{
string msg = "是否添加此条数据?";
if (1 == (int)MessageBox.Show(msg, "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Exclamation))
{
MySqlCommandBuilder builder = new MySqlCommandBuilder(mda);
mda.Update(ds, "user");
MessageBox.Show("添加成功", "提示");
}
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString(), "error!");
}
}
private void button4_Click(object sender, EventArgs e, object ds)
{
if (mda == null || ds == null)
{
MessageBox.Show("请先导入数据!");
return;
}
try
{
string msg = "是否修改此条数据?";
if (1 == (int)MessageBox.Show(msg, "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Exclamation))
{
MySqlCommandBuilder builder = new MySqlCommandBuilder(mda); //命令生成器。
//适配器会自动更新用户在表上的操作到数据库中
mda.Update(ds, "user");
MessageBox.Show("修改成功", "提示");
}
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString(), "error!");
}
}
private void button5_Click(object sender, EventArgs e, object conn)
{
int index = dataGridView1.CurrentCell.RowIndex;
int id = (int)dataGridView1.Rows[index].Cells[0].Value;
string sql = "delete from user where id=" + id + "";
conn.Open();
MySqlCommand cmd = conn.CreateCommand();
cmd.CommandText = sql;
int i = cmd.ExecuteNonQuery();
if (i < 0)
{
conn.Close();
MessageBox.Show("error!");
return;
}
conn.Close();
}
private void button6_Click(object sender, EventArgs e)
{
Application.Exit();
}
}
}
仓库地址