資源簡介
C#操作Mysql實例demo實現(xiàn)簡單的增刪改查,非常適合剛學C#的新手。簡單的操作數(shù)據(jù)庫的增刪改查。C#新手入們demo

代碼片段和文件信息
using?System;
using?System.Collections.Generic;
using?System.ComponentModel;
using?System.Data;
using?System.Drawing;
using?System.Linq;
using?System.Text;
using?System.Windows.Forms;
using?MySql.Data.MySqlClient;
namespace?測試與mysql的連接
{
????public?partial?class?Form1?:?Form
????{
????????MySqlDataAdapter?da;
????????string?sql;
????????DataTable?dt;
????????MySqlDataReader?dr;
????????MySqlCommand?cmd;
????????MySqlConnection?myConn?=?new?MySqlConnection(“server=localhost;user?id=root;?password=root;?database=test;?pooling=false;port=3306“);
????????public?Form1()
????????{
????????????InitializeComponent();
????????}
????????
????????//查詢
????????private?void?button1_Click(object?sender?EventArgs?e)
????????{
????????????try
????????????{
????????????????myConn.Open();
????????????????sql?=?“select?*?from?user“;
????????????????da?=?new?MySqlDataAdapter(sql?myConn);
????????????????dt?=?new?DataTable();
????????????????da.Fill(dt);
????????????????dataGridView1.DataSource?=?dt;
????????????}
????????????catch?(Exception?ex)
????????????{
????????????????MessageBox.Show(ex.Message);
????????????}
????????????finally
????????????{
????????????????myConn.Close();
????????????}
????????}
????????private?void?button4_Click(object?sender?EventArgs?e)
????????{
????????????try
????????????{
????????????????cmd?=?myConn.CreateCommand();
????????????????myConn.Open();
????????????????cmd.CommandText?=?“delete?from?user?where?userid=@id“;//刪除語句,已ID為條件刪除
????????????????cmd.Parameters.Add(“@id“?MySqlDbType.Int32).Value?=?textBox3.Text;
????????????????cmd.ExecuteNonQuery();
????????????????MessageBox.Show(“刪除成功!“);
????????????}
????????????catch?(Exception?ex)
????????????{
????????????????MessageBox.Show(ex.Message);
????????????}
????????????finally
????????????{
????????????????myConn.Close();
????????????}
????????}
????????//增加
????????private?void?button3_Click(object?sender?EventArgs?e)
????????{
????????????try
????????????{
????????????????cmd?=?myConn.CreateCommand();//sql命令對象,表示要對sql數(shù)據(jù)庫執(zhí)行一個sql語句
????????????????cmd.CommandText?=?“insert?into?user(username)?values(@name)“;//sql語句@name@age表示兩個參數(shù)
????????????????cmd.Parameters.Add(“@name“?MySqlDbType.VarChar).Value?=?textBox6.Text;
????????????????myConn.Open();//打開連接
????????????????cmd.ExecuteNonQuery();//執(zhí)行不是查詢的sql語句
????????????????MessageBox.Show(“插入成功!“);
????????????}//打開數(shù)據(jù)庫連接一定要記得關(guān)閉連接:
????????????catch?(Exception?ex)//由于sql語句對半全角很敏感,捕捉異常
????????????{
????????????????MessageBox.Show(ex.Message);
????????????}
????????????finally
????????????{
????????????????myConn.Close();//不管打開成功還是失敗,都能關(guān)閉連接
????????????}
????????}
?????????//修改查詢
????????private?void?button2_Click(object?sender?EventArgs?e)
????????{
???????????try
???????????{
?????????????????cmd?=?myConn.CreateCommand();
?????????????????cmd.CommandText?=?“select?username?from?user?where?userid=@id“;
???????????????
?屬性????????????大小?????日期????時間???名稱
-----------?---------??----------?-----??----
????.......???4206748??2009-04-23?01:42??winform簡單的增刪改查\mysql-connector-net-6.0.3\mysql.data.msi
?????文件???????7730??2012-06-16?10:08??winform簡單的增刪改查\數(shù)據(jù)庫表截圖.jpg
?????文件???????4570??2012-06-16?10:10??winform簡單的增刪改查\簡單的增刪改查\Backup\測試與mysql的連接\Form1.cs
?????文件??????13159??2012-06-16?09:49??winform簡單的增刪改查\簡單的增刪改查\Backup\測試與mysql的連接\Form1.Designer.cs
?????文件???????5814??2012-06-16?09:49??winform簡單的增刪改查\簡單的增刪改查\Backup\測試與mysql的連接\Form1.resx
?????文件????????504??2012-06-15?15:41??winform簡單的增刪改查\簡單的增刪改查\Backup\測試與mysql的連接\Program.cs
?????文件???????1378??2012-06-15?15:41??winform簡單的增刪改查\簡單的增刪改查\Backup\測試與mysql的連接\Properties\AssemblyInfo.cs
?????文件???????2896??2012-06-15?15:41??winform簡單的增刪改查\簡單的增刪改查\Backup\測試與mysql的連接\Properties\Resources.Designer.cs
?????文件???????5612??2012-06-15?15:41??winform簡單的增刪改查\簡單的增刪改查\Backup\測試與mysql的連接\Properties\Resources.resx
?????文件???????1108??2012-06-15?15:41??winform簡單的增刪改查\簡單的增刪改查\Backup\測試與mysql的連接\Properties\Settings.Designer.cs
?????文件????????249??2012-06-15?15:41??winform簡單的增刪改查\簡單的增刪改查\Backup\測試與mysql的連接\Properties\Settings.settings
?????文件???????4009??2012-06-15?16:09??winform簡單的增刪改查\簡單的增刪改查\Backup\測試與mysql的連接\簡單的增刪改查.csproj
?????文件????????955??2012-06-16?10:09??winform簡單的增刪改查\簡單的增刪改查\Backup\簡單的增刪改查.sln
????..A..H.?????15360??2016-12-25?16:49??winform簡單的增刪改查\簡單的增刪改查\Backup\簡單的增刪改查.v12.suo
?????文件??????14336??2012-06-16?10:11??winform簡單的增刪改查\簡單的增刪改查\測試與mysql的連接\bin\Debug\測試與mysql的連接.exe
?????文件??????32256??2012-06-16?10:11??winform簡單的增刪改查\簡單的增刪改查\測試與mysql的連接\bin\Debug\測試與mysql的連接.pdb
?????文件??????22704??2016-12-25?16:50??winform簡單的增刪改查\簡單的增刪改查\測試與mysql的連接\bin\Debug\測試與mysql的連接.vshost.exe
?????文件????????490??2016-07-16?19:44??winform簡單的增刪改查\簡單的增刪改查\測試與mysql的連接\bin\Debug\測試與mysql的連接.vshost.exe.manifest
?????文件???????4570??2012-06-16?10:10??winform簡單的增刪改查\簡單的增刪改查\測試與mysql的連接\Form1.cs
?????文件??????13159??2012-06-16?09:49??winform簡單的增刪改查\簡單的增刪改查\測試與mysql的連接\Form1.Designer.cs
?????文件???????5814??2012-06-16?09:49??winform簡單的增刪改查\簡單的增刪改查\測試與mysql的連接\Form1.resx
?????文件????????791??2016-12-25?16:50??winform簡單的增刪改查\簡單的增刪改查\測試與mysql的連接\obj\Debug\DesignTimeResolveAssemblyReferences.cache
?????文件???????7010??2016-12-25?16:50??winform簡單的增刪改查\簡單的增刪改查\測試與mysql的連接\obj\Debug\DesignTimeResolveAssemblyReferencesInput.cache
?????文件??????66486??2012-06-15?16:09??winform簡單的增刪改查\簡單的增刪改查\測試與mysql的連接\obj\Debug\ResolveAssemblyReference.cache
?????文件???????4608??2016-12-25?16:50??winform簡單的增刪改查\簡單的增刪改查\測試與mysql的連接\obj\Debug\TempPE\Properties.Resources.Designer.cs.dll
?????文件????????908??2012-06-16?10:06??winform簡單的增刪改查\簡單的增刪改查\測試與mysql的連接\obj\Debug\測試與mysql的連接.csproj.FileListAbsolute.txt
?????文件????????847??2012-06-16?09:55??winform簡單的增刪改查\簡單的增刪改查\測試與mysql的連接\obj\Debug\測試與mysql的連接.csproj.GenerateResource.Cache
?????文件??????14336??2012-06-16?10:11??winform簡單的增刪改查\簡單的增刪改查\測試與mysql的連接\obj\Debug\測試與mysql的連接.exe
?????文件????????180??2012-06-16?09:55??winform簡單的增刪改查\簡單的增刪改查\測試與mysql的連接\obj\Debug\測試與mysql的連接.Form1.resources
?????文件??????32256??2012-06-16?10:11??winform簡單的增刪改查\簡單的增刪改查\測試與mysql的連接\obj\Debug\測試與mysql的連接.pdb
............此處省略30個文件信息
評論
共有 條評論