c# mysql 操作_c#对mysql数据库的基本操作
1 數據庫的創建
打開已經安裝好的數據庫,如下流程:
step 1:單擊 “MySQL Command Line Client-Unicode”
step 2:輸入密碼,進入數據庫
step 3:建立一個簡單的數據庫,在這里建立一個名稱為“mysql_test”的數據庫,如下所示:
不過注意此時的數據庫里面還沒有任何東西。
step 4:在數據庫創建一個數據表,如下所示:
use mysql_test;
create table student(
id int not null auto_increment,
StuName varchar(5) not null,
StoNo varchar(14) not null,
Age varchar(3) not null,
primary id
)engine = InnoDB default charset utf=8;
describe student;
insert into student(StuName,StoNo,Age)
values
('張三',3120150802200,18),
('李四',3120150802201,18),
('王麻子',3120150802202,18),
('百度',3120150802203,19),
('阿里',3120150802204,19),
('騰訊',3120150802205,20);
select * from student;
2 連接數據庫
使用的是“窗體控件” 來實現連接數據庫
step 1:新建一個“窗體控件”項目,從工具箱拖進“button ”公共控件
step 2:在解決方案資源管理器一欄,找到引用,并添加引用“MySql.Data.dll ”
step 3:雙擊控件,進入程序設計,添加:using MySql.Data.MySqlClient;
step 4:加接代碼如下:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using MySql.Data.MySqlClient;
namespace mysql_connect
{
public partial class UserControl1: UserControl
{
public UserControl1()
{
InitializeComponent();
}
static string conStr = "server=localhost;port=3306;user=root;password=******; database=mysql_test;";//password 輸入你所建立數據庫的密碼
private void Mysql_connect_Click(object sender, EventArgs e)
{
MySqlConnection connect = new MySqlConnection(conStr);
try
{
connect.Open();//建立連接,可能出現異常,使用try catch語句 MessageBox.Show("恭喜,已經建立連接!");
}
catch (MySqlException exe)
{
MessageBox.Show(exe.Message);//有錯則報出錯誤 }
finally
{
connect.Close();//關閉通道 }
}
}
}
3 數據操作
數據庫連接成功之后,就可以使用SQL語句對數據庫進行命令操作了。command類提供了幾個可執行的命令,下面分別介紹。
流程如下:
第一,使用SqlConnection對象連接數據庫;
第二,建立SqlCommand對象,負責SQL語句的執行和存儲過程的調用;
第三,對SQL或存儲過程執行后返回的“結果”進行操作。
3.1 ExecuteNonQuery(): 執行一個命令,但不返回任何結果,就是執行非查詢語句,如:Update:更新;Insert:插入;Delete:刪除。
//Update更新代碼
string SqlString = "Update ff "
+" Set username='李四',password='20191001'"
+" where id='2'";//SQL語句
MySqlCommand cmd = new MySqlCommand(SqlString,connect);//建立數據庫命令,確定sql數據操作語句,和數據庫連接。
int ret = cmd.ExecuteNonQuery();//執行SQL語句
MessageBox.Show("執行成功,影響了"+ ret.ToString() + "條數據!");
3.2 ExecuteReader():執行一個命令,返回一個類型化的IDataReader;執行查詢語句的命令,也就是select語句。
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using MySql.Data.MySqlClient;
namespace mysql_read1
{
public partial class UserControl1: UserControl
{
public UserControl1()
{
InitializeComponent();
}
static string conStr = "server=localhost;port=3306;user=root;password=11xxjlw520; database=mysql_test;";//password 輸入你所建立數據庫的密碼 private void Button1_Click(object sender, EventArgs e)
{
MySqlConnection connect = new MySqlConnection(conStr);
try
{
connect.Open();//建立連接,可能出現異常,使用try catch語句 string SqlStr = "select StuName,StuNo,Age from student where StuNo='3120150802202'";
MySqlCommand cmd = new MySqlCommand(SqlStr, connect);
MySqlDataReader DataReader = cmd.ExecuteReader();
while (DataReader.Read())
{
Console.WriteLine(DataReader.GetString("StuName") + "\t" + DataReader.GetString("StuNo") + "\t"
+ "\t" + DataReader.GetString("Age"));//"userid"是數據庫對應的列名,推薦這種方式 }
}
catch (MySqlException exe)
{
MessageBox.Show(exe.Message);//有錯則報出錯誤 }
finally
{
connect.Close();//關閉通道 }
}
}
}
3.3 ExecuteScalar:執行一個命令,返回一個值。
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using MySql.Data.MySqlClient;
namespace mysql
{
public partial class UserControl1: UserControl
{
public UserControl1()
{
InitializeComponent();
}
static string conStr = "server=localhost;port=3306;user=root;password=11xxjlw520; database=mysql_test;";//password 輸入你所建立數據庫的密碼
private void Button1_Click(object sender, EventArgs e)
{
MySqlConnection connect = new MySqlConnection(conStr);
try
{
connect.Open();//建立連接,可能出現異常,使用try catch語句
string SqlStr = "select now()";
MySqlCommand cmd = new MySqlCommand(SqlStr, connect);
object Ret = cmd.ExecuteScalar();
MessageBox.Show("數據庫服務器當前系統時間是:"+Ret.ToString());
}
catch (MySqlException exe)
{
MessageBox.Show(exe.Message);//有錯則報出錯誤
}
finally
{
connect.Close();//關閉通道
}
}
}
}
4.數據綁定:將我們需要的數據與顯示的控件聯系在一起。下面將實現使用DataGridView控件來顯示連接數據源的詳細數據。
step 1:創建Windows窗體程序
step 2:建立和數據庫mysql_test的連接(向導法和程序法)
總結
以上是生活随笔為你收集整理的c# mysql 操作_c#对mysql数据库的基本操作的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: debian 重复执行sh_debian
- 下一篇: Linux下好用的日志库,我使用過的Li