用C#.net连接MYSQL,怎么连接?还要安装什么吗?急!!!

用C#.net连接MYSQL,怎么连接?还要安装什么吗?急!!!,第1张

连接MYSQL数据库的方法及示例

方法一:

使用MYSQL推出的MySQL

Connector/Net

is

an

ADO.NET

driver

for

MySQL

该组件为MYSQL为ADO.NET访问MYSQL数据库设计的.NET访问组件。

安装完成该组件后,引用命名空间MySql.Data.MySqlClient

使用命令行编译时:csc

/r:MySql.Data.dll

test.cs

方法二:

通过ODBC访问MYSQL数据库

访问前要先下载两个组件:odbc.net和MYSQL的ODBC驱动(MySQL

Connector/ODBC

(MyODBC)

driver)目前为3.51版

安装完成后,即可通过ODBC访问MYSQL数据库

方法三:

使用CoreLab推出的MYSQL访问组件,面向.NET

安装完成后,引用命名空间:CoreLab.MySql

使用命令编译时:csc

/r:CoreLab.MySql.dll

test.cs

以下为访问MYSQL数据库实例

编译指令:csc

/r:CoreLab.MySql.dll

/r:MySql.Data.dll

test.cs

using

System

using

System.Net

using

System.Text

using

CoreLab.MySql

using

System.Data.Odbc

using

MySql.Data.MySqlClient

class

ConnectMySql

{

public

void

Connect_CoreLab()

{

string

c

MySqlConnection

mycn

=

new

MySqlConnection(constr)

mycn.Open()

MySqlCommand

mycm

=

new

MySqlCommand("select

*

from

shop",mycn)

MySqlDataReader

msdr

=

mycm.ExecuteReader()

while(msdr.Read())

{

if

(msdr.HasRows)

{

Console.WriteLine(msdr.GetString(0))

}

}

msdr.Close()

mycn.Close()

}

public

void

Connect_Odbc()

{

//string

MyC

string

MyC

+

"SERVER=localhost"

+

"DATABASE=test"

+

"UID=root"

+

"PASSWORD=qing"

+

"OPTION=3"

OdbcConnection

MyConn

=

new

OdbcConnection(MyConString)

MyConn.Open()

OdbcCommand

mycm

=

new

OdbcCommand("select

*

from

hello",MyConn)

OdbcDataReader

msdr

=

mycm.ExecuteReader()

while(msdr.Read())

{

if

(msdr.HasRows)

{

Console.WriteLine(msdr.GetString(0))

}

}

msdr.Close()

MyConn.Close()

}

public

void

Connect_Net()

{

string

myC

MySqlConnection

mycn

=

new

MySqlConnection(myConnectionString)

mycn.Open()

MySqlCommand

mycm

=

new

MySqlCommand("select

*

from

hello",mycn)

MySqlDataReader

msdr

=

mycm.ExecuteReader()

while(msdr.Read())

{

if

(msdr.HasRows)

{

Console.WriteLine(msdr.GetString(0))

}

}

msdr.Close()

mycn.Close()

}

public

static

void

Main()

{

ConnectMySql

ms

=

new

ConnectMySql()

ms.Connect_CoreLab()

ms.Connect_Odbc()

Connect_Net()

}

}

MySql安装

yum -y install libaio

wget https://cdn.mysql.com//Downloads/MySQL-5.7/mysql-5.7.24-linux-glibc2.12-x86_64.tar.gz

cd /usr/local/soft/mysql/src

mkdir ../5.7.24_3306 &&tar -xzvf mysql-5.7.24-linux-glibc2.12-x86_64.tar.gz -C ../5.7.24_3306 --strip-components 1

mkdir -p /usr/local/soft/mysql/5.7.24_3306/{run,data,logs,bin_log,sock,cert}

groupadd mysql &&useradd -r -g mysql -s /bin/false mysql

cd /usr/local/soft/mysql/5.7.24_3306/bin &&./mysqld --initialize

--explicit_defaults_for_timestamp

--user=mysql

--basedir=/usr/local/soft/mysql/5.7.24_3306

--datadir=/usr/local/soft/mysql/5.7.24_3306/data

--lc_messages_dir=/usr/local/soft/mysql/5.7.24_3306/share

--lc_messages=en_US

2018-12-01T09:56:45.934627Z 0 [Warning] InnoDB: New log files created, LSN=45790

2018-12-01T09:56:46.056837Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.

2018-12-01T09:56:46.127141Z 0 [Warning] No existing UUID has been found, so we assume that this is the first time that this server has been started. Generating a new UUID: 69d028f9-f54f-11e8-9c4a-00163e08e793.

2018-12-01T09:56:46.130249Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.

2018-12-01T09:56:46.131377Z 1 [Note] A temporary password is generated for root@localhost: sDdPx3>.,oBi

cd /usr/local/soft/mysql/5.7.24_3306/bin &&./mysql_ssl_rsa_setup --datadir=/usr/local/soft/mysql/5.7.24_3306/cert

修改证书权限为644

cd /usr/local/soft/mysql/5.7.24_3306/bin &&./mysql -uroot -S /usr/local/soft/mysql/5.7.24_3306/sock/mysql.sock

use mysql

update user set host = '%', authentication_string = password('123456') where user = 'root'

select host,user,authentication_string from user

flush privileges

grant all privileges on . to 'root'@'%'

flush privileges

cat >/etc/systemd/system/3306.service <<EOF

[Unit]

Description=MySQL 3306

Documentation= http://dev.mysql.com/doc/refman/en/using-systemd.html

After=network.target remote-fs.target nss-lookup.target

[Service]

Type=forking

PIDFile=/usr/local/soft/mysql/5.7.24_3306/run/mysql.pid

ExecStart=/usr/local/soft/mysql/5.7.24_3306/support-files/mysql.server start

ExecReload=/usr/local/soft/mysql/5.7.24_3306/support-files/mysql.server restart

ExecStop=/usr/local/soft/mysql/5.7.24_3306/support-files/mysql.server stop

PrivateTmp=true

[Install]

WantedBy=multi-user.target

EOF

systemctl enable 3306.service

1、

cd /usr/local/soft/mysql/5.7.24.3306/bin &&./mysqld_safe --skip-grant-tables &

cd /usr/local/soft/mysql/5.7.24.3306/bin &&./mysqld_safe --skip-grant-tables --skip-networking &

2、

./mysql -uroot -p -S /usr/local/soft/mysql/5.7.24.3306/sock/mysql.sock

3、

use mysql

update mysql.user set authentication_string=password('123456') where user='root'

flush privileges

mkdir -p /usr/local/soft/mysql/back/5.7.23_3306 &&cp -Rf /usr/local/soft/mysql/5.7.23_3306/* /usr/local/soft/mysql/back/5.7.23_3306

mkdir -p /usr/local/soft/mysql/back/5.7.24_3306 &&cp -Rf /usr/local/soft/mysql/5.7.24_3306/* /usr/local/soft/mysql/back/5.7.24_3306

CREATE USER 'sdyc'@'%' IDENTIFIED BY '123456'

GRANT SELECT, INSERT, UPDATE, DELETE, EXECUTE ON sd_data.* TO 'sdyc'@'%'

GRANT SELECT, INSERT, UPDATE, DELETE, EXECUTE ON card_data.* TO 'sdyc'@'%'

SHOW GRANTS FOR sdyc

CREATE USER 'back'@'localhost' IDENTIFIED BY '123456'

GRANT SELECT, RELOAD, PROCESS, SHOW DATABASES, SUPER, LOCK TABLES, REPLICATION CLIENT ON . TO 'back'@'localhost'

SHOW GRANTS FOR back

REVOKE SELECT, INSERT, UPDATE, DELETE, EXECUTE ON sd_data.* FROM 'sdyc'@'%'

方法/步骤

MySQL安装文件分为两种,一种是msi格式的,一种是zip格式的。如果是msi格式的可以直接点击安装,按照它给出的安装提示进行安装(相信大家的英文可以看懂英文提示),一般MySQL将会安装在C:\Program Files\MySQL\MySQL Server 5.6 该目录中;zip格式是自己解压,解压缩之后其实MySQL就可以使用了,但是要进行配置。

解压之后可以将该文件夹改名,放到合适的位置,个人建议把文件夹改名为MySQL Server 5.6,放到C:\Program Files\MySQL路径中。当然你也可以放到自己想放的任意位置。

完成上述步骤之后,很多用户开始使用MySQL,但会出现图示的错误。这是因为没有配置环境变量所致。配置环境变量很简单:

我的电脑->属性->高级->环境变量

选择PATH,在其后面添加: 你的mysql bin文件夹的路径 (如:C:\Program Files\MySQL\MySQL Server 5.6\bin )

PATH=.......C:\Program Files\MySQL\MySQL Server 5.6\bin (注意是追加,不是覆盖)

配置完环境变量之后先别忙着启动mysql,我们还需要修改一下配置文件(如果没有配置,之后启动的时候就会出现图中的错误哦!:错误2 系统找不到文件),mysql-5.6.1X默认的配置文件是在C:\Program Files\MySQL\MySQL Server 5.6\my-default.ini,或者自己建立一个my.ini文件,

在其中修改或添加配置(如图):

[mysqld]

basedir=C:\Program Files\MySQL\MySQL Server 5.6(mysql所在目录)

datadir=C:\Program Files\MySQL\MySQL Server 5.6\data (mysql所在目录\data)

以管理员身份运行cmd(一定要用管理员身份运行,不然权限不够),

输入:cd C:\Program Files\MySQL\MySQL Server 5.6\bin 进入mysql的bin文件夹(不管有没有配置过环境变量,也要进入bin文件夹,否则之后启动服务仍然会报错误2)

输入mysqld -install(如果不用管理员身份运行,将会因为权限不够而出现错误:Install/Remove of the Service Denied!)

安装成功

安装成功后就要启动服务了,继续在cmd中输入:net start mysql(如图),服务启动成功!

此时很多人会出现错误,请看注意:

注意:这个时候经常会出现错误2和错误1067。

如果出现“错误2 系统找不到文件”,检查一下是否修改过配置文件或者是否进入在bin目录下 *** 作,如果配置文件修改正确并且进入了bin文件夹,需要先删除mysql(输入 mysqld -remove)再重新安装(输入 mysqld -install);

如果出现错误1067,那就是配置文件修改错误,确认一下配置文件是否正确。

服务启动成功之后,就可以登录了,如图,输入mysql -u root -p(第一次登录没有密码,直接按回车过),登录成功!


欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/zaji/7678656.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-04-08
下一篇 2023-04-08

发表评论

登录后才能评论

评论列表(0条)

保存