Winform连接MySQL数据库实现增删查改功能
这个练⼿⼩项⽬是今年8⽉刚接触C#的时候做的...当时对C#还不熟,边查资料边完成的,其实有很多需要优化的地⽅...数据库是提前创建好的,⽤户名密码什么的直接写在点击连接按钮事件中了,可能未来的某⼀天想起来这个⼩项⽬会回来完善⼀下
界⾯
代码
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
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 test
{前端微服务
public partial class Form1 : Form
{
MySqlConnection SQLCon;
MySqlCommand SQLCmd;
int s1;
string s2 = "null", s3 = "null", s4 = "null", s5 = "null", s6 = "null";
public Form1()
{
InitializeComponent();
}
//连接
private void bt_CONNECT_Click(object sender, EventArgs e)
{
string connetStr = "server=localhost;user=root;password=admin;database=testdb;sslMode=none;"; //localhost不⽀持ssl连接时,最后⼀句⼀定要加 SQLCon = new MySqlConnection(connetStr);
try
{
SQLCon.Open(); //连接数据库
//MessageBox.Show("数据库连接成功", "提⽰", MessageBoxButtons.OK);
string searchStr = "select * from student"; //student表中数据
MySqlDataAdapter adapter = new MySqlDataAdapter(searchStr, SQLCon);
DataTable a = new DataTable();
adapter.Fill(a);
this.dataGridView1.DataSource = a;
}
catch (MySqlException ex)
{
MessageBox.Show(ex.Message, "提⽰", MessageBoxButtons.OK); //显⽰错误信息
}
}
//增加
private void bt_ADD_Click(object sender, EventArgs e)
{
s1 = Convert.ToInt16(textBox1.Text);
s2 = textBox2.Text;
s3 = textBox3.Text;
s4 = textBox4.Text;
s5 = textBox5.Text;
s6 = textBox6.Text;
try
{
string searchStr = "insert into student values(" + s1 + ",'" + s2 + "','" + s3 + "','" + s4 + "','" + s5 + "','" + s6 + "')";
SQLCmd = new MySqlCommand(searchStr, SQLCon);
SQLCmd.ExecuteNonQuery();
MessageBox.Show("插⼊成功", "提⽰", MessageBoxButtons.OK);
fraction可数吗bt_CONNECT_Click(sender, e);
}
catch (MySqlException ex)
{
汇编语言masm怎么用MessageBox.Show(ex.Message, "提⽰", MessageBoxButtons.OK);
}
html底部代码}
//删除
private void bt_DELETE_Click(object sender, EventArgs e)
{
if (textBox1.Text.Trim() != "")
{
try
{
s1 = Convert.ToInt16(textBox1.Text);
string searchStr = ("delete from student where id=" + s1);
SQLCmd = new MySqlCommand(searchStr, SQLCon);
SQLCmd.ExecuteNonQuery();
MessageBox.Show("删除成功!", "提⽰", MessageBoxButtons.OK);
bt_CONNECT_Click(sender, e);
}
catch (MySqlException ex)
{
MessageBox.Show(ex.Message, "提⽰", MessageBoxButtons.OK);
}
}
else
{
try
{
for (int i = 0; i < 100; i++)//遍历所有选中的⾏
{
if (dataGridView1.Rows[i].Selected == true)
{
string value = dataGridView1.Rows[i].Cells[0].Value.ToString();
int tmp = Convert.ToInt32(value);
string sql = ("delete from student where id=" + tmp);
SQLCmd = new MySqlCommand(sql, SQLCon);
SQLCmd.ExecuteNonQuery();
MessageBox.Show("删除成功!", "提⽰", MessageBoxButtons.OK); bt_CONNECT_Click(sender, e);
mysql菜鸟教程增删改查break;
}
}
}
catch (MySqlException ex)
{
MessageBox.Show(ex.Message, "提⽰", MessageBoxButtons.OK);
}
}
}
//查询
private void bt_SEARCH_Click(object sender, EventArgs e)
{
//s1 = Convert.ToInt16(textBox1.Text);
string str = "";
if (textBox1.Text.Trim() != "")
str = "where id like " + textBox1.Text.Trim();
try
{
string searchStr = ("select * from student " + str);
MySqlDataAdapter adapter = new MySqlDataAdapter(searchStr, SQLCon); DataTable a = new DataTable();
adapter.Fill(a);
this.dataGridView1.DataSource = a;
}
catch (MySqlException ex)
{
MessageBox.Show(ex.Message, "提⽰", MessageBoxButtons.OK);
}
}
}
//修改
特斯拉事件对特斯拉有何影响private void bt_MODIFY_Click(object sender, EventArgs e)
{
s1 = Convert.ToInt16(textBox1.Text);
try
{
string searchStr = ("update student set gender='123' where id=" + s1); SQLCmd = new MySqlCommand(searchStr, SQLCon);
SQLCmd.ExecuteNonQuery();
bt_CONNECT_Click(sender, e);
}
catch (MySqlException ex)
{
MessageBox.Show(ex.Message, "提⽰", MessageBoxButtons.OK);
}
}
//断开
private void bt_DISCONNECT_Click(object sender, EventArgs e)
{
SQLCon.Close(); //断开连接
}
}
}
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论