C#链接MySQL
时间:2021-07-01 10:21:17
帮助过:4人阅读
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 MySQLtest
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void btnConnect_Click(
object sender, EventArgs e)
{
string strcn =
"Server=127.0.0.1;User ID=root;Password=mysql;Database=student;CharSet=gbk;";
MySqlConnection cn =
new MySqlConnection(strcn);
//实例化链接
cn.Open();
//开启连接
MySqlCommand cmd =
cn.CreateCommand();
/************************************************
cmd.CommandText = "insert stu (sno,sname,sclass) values(‘20161‘,‘tt‘,‘class1‘)";//SQL插入数据
cmd.CommandText = "insert stu values(‘20162‘,‘ll‘,‘class3‘)";
cmd.CommandText = "delete from stu";//删除数据
cmd.CommandText = "delete from stu where sname=‘ll‘";
cmd.CommandText = "update stu set sclass=‘class2‘ where sclass=‘class3‘";//修改数据
************************************************/
cmd.CommandType =
CommandType.Text;
int i =
cmd.ExecuteNonQuery();
cn.Close();//关闭连接
}
}
}
上课学的C#链接Sql Server 因为一直用的MySQL 自己把它改成了C#链接MySQL
C#链接MySQL
标签: