日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 运维知识 > 数据库 >内容正文

数据库

c# mysql 操作_c#对mysql数据库的基本操作

發(fā)布時(shí)間:2025/3/15 数据库 41 豆豆
生活随笔 收集整理的這篇文章主要介紹了 c# mysql 操作_c#对mysql数据库的基本操作 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

1 數(shù)據(jù)庫的創(chuàng)建

打開已經(jīng)安裝好的數(shù)據(jù)庫,如下流程:

step 1:單擊 “MySQL Command Line Client-Unicode”

step 2:輸入密碼,進(jìn)入數(shù)據(jù)庫

step 3:建立一個(gè)簡單的數(shù)據(jù)庫,在這里建立一個(gè)名稱為“mysql_test”的數(shù)據(jù)庫,如下所示:

不過注意此時(shí)的數(shù)據(jù)庫里面還沒有任何東西。

step 4:在數(shù)據(jù)庫創(chuàng)建一個(gè)數(shù)據(jù)表,如下所示:

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 連接數(shù)據(jù)庫

使用的是“窗體控件” 來實(shí)現(xiàn)連接數(shù)據(jù)庫

step 1:新建一個(gè)“窗體控件”項(xiàng)目,從工具箱拖進(jìn)“button ”公共控件

step 2:在解決方案資源管理器一欄,找到引用,并添加引用“MySql.Data.dll ”

step 3:雙擊控件,進(jìn)入程序設(shè)計(jì),添加: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 輸入你所建立數(shù)據(jù)庫的密碼

private void Mysql_connect_Click(object sender, EventArgs e)

{

MySqlConnection connect = new MySqlConnection(conStr);

try

{

connect.Open();//建立連接,可能出現(xiàn)異常,使用try catch語句 MessageBox.Show("恭喜,已經(jīng)建立連接!");

}

catch (MySqlException exe)

{

MessageBox.Show(exe.Message);//有錯(cuò)則報(bào)出錯(cuò)誤 }

finally

{

connect.Close();//關(guān)閉通道 }

}

}

}

3 數(shù)據(jù)操作

數(shù)據(jù)庫連接成功之后,就可以使用SQL語句對(duì)數(shù)據(jù)庫進(jìn)行命令操作了。command類提供了幾個(gè)可執(zhí)行的命令,下面分別介紹。

流程如下:

第一,使用SqlConnection對(duì)象連接數(shù)據(jù)庫;

第二,建立SqlCommand對(duì)象,負(fù)責(zé)SQL語句的執(zhí)行和存儲(chǔ)過程的調(diào)用;

第三,對(duì)SQL或存儲(chǔ)過程執(zhí)行后返回的“結(jié)果”進(jìn)行操作。

3.1 ExecuteNonQuery(): 執(zhí)行一個(gè)命令,但不返回任何結(jié)果,就是執(zhí)行非查詢語句,如:Update:更新;Insert:插入;Delete:刪除。

//Update更新代碼

string SqlString = "Update ff "

+" Set username='李四',password='20191001'"

+" where id='2'";//SQL語句

MySqlCommand cmd = new MySqlCommand(SqlString,connect);//建立數(shù)據(jù)庫命令,確定sql數(shù)據(jù)操作語句,和數(shù)據(jù)庫連接。

int ret = cmd.ExecuteNonQuery();//執(zhí)行SQL語句

MessageBox.Show("執(zhí)行成功,影響了"+ ret.ToString() + "條數(shù)據(jù)!");

3.2 ExecuteReader():執(zhí)行一個(gè)命令,返回一個(gè)類型化的IDataReader;執(zhí)行查詢語句的命令,也就是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 輸入你所建立數(shù)據(jù)庫的密碼 private void Button1_Click(object sender, EventArgs e)

{

MySqlConnection connect = new MySqlConnection(conStr);

try

{

connect.Open();//建立連接,可能出現(xiàn)異常,使用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"是數(shù)據(jù)庫對(duì)應(yīng)的列名,推薦這種方式 }

}

catch (MySqlException exe)

{

MessageBox.Show(exe.Message);//有錯(cuò)則報(bào)出錯(cuò)誤 }

finally

{

connect.Close();//關(guān)閉通道 }

}

}

}

3.3 ExecuteScalar:執(zhí)行一個(gè)命令,返回一個(gè)值。

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 輸入你所建立數(shù)據(jù)庫的密碼

private void Button1_Click(object sender, EventArgs e)

{

MySqlConnection connect = new MySqlConnection(conStr);

try

{

connect.Open();//建立連接,可能出現(xiàn)異常,使用try catch語句

string SqlStr = "select now()";

MySqlCommand cmd = new MySqlCommand(SqlStr, connect);

object Ret = cmd.ExecuteScalar();

MessageBox.Show("數(shù)據(jù)庫服務(wù)器當(dāng)前系統(tǒng)時(shí)間是:"+Ret.ToString());

}

catch (MySqlException exe)

{

MessageBox.Show(exe.Message);//有錯(cuò)則報(bào)出錯(cuò)誤

}

finally

{

connect.Close();//關(guān)閉通道

}

}

}

}

4.數(shù)據(jù)綁定:將我們需要的數(shù)據(jù)與顯示的控件聯(lián)系在一起。下面將實(shí)現(xiàn)使用DataGridView控件來顯示連接數(shù)據(jù)源的詳細(xì)數(shù)據(jù)。

step 1:創(chuàng)建Windows窗體程序

step 2:建立和數(shù)據(jù)庫mysql_test的連接(向?qū)Хê统绦蚍?

總結(jié)

以上是生活随笔為你收集整理的c# mysql 操作_c#对mysql数据库的基本操作的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。