.Net连接MySQL数据库

MySQL provides standards-based drivers for JDBC, ODBC, and .Net enabling developers to build database applications in their language of choice. In addition, a native C library allows developers to embed MySQL directly into their applications.
连接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 Connector/Net

TYPE  .NET Framework Class Library
USAGE  MySql.Data.MySqlClient.MySqlConnection
MANUFACTURER  MySQL

Standard

Server=localhost;Database=UserInfo;Uid=root;Pwd=xian80;

Default port is 3306.

 
 

Specifying port

Server=localhost;Port=1234;Database=UserInfo;Uid=root;Pwd=xian80;
 

Named pipes

Server=localhost;Port=-1;Database=UserInfo;Uid=root;Pwd=xian80;

It is the port value of -1 that tells the driver to use named pipes network protocol. This is available on Windows only. The value is ignored if Unix socket is used.

 
 

Multiple servers

Use this to connect to a server in a replicated server configuration without concern on which server to use.

Server=serverAddress1 & serverAddress2 & etc..;Database=UserInfo;Uid=root;Pwd=xian80;
 
 

Using encryption

This one activates SSL encryption for all data sent between the client and server. The server needs to have a certificate installed.

Server=localhost;Database=UserInfo;Uid=root;Pwd=xian80;Encryption=true;

This option is available from Connector/NET version 5.0.3. In earlier versions, this option has no effect.

 
 

Using encryption, alternative

Some reported problems with the above one. Try replacing the key "Encryption" with "Encrypt" instead.

Server=localhost;Database=UserInfo;Uid=root;Pwd=xian80;Encrypt=true;


这里我使用第一种方法进行示例,首先要到  http://www.mysql.com/downloads/connector/net/

    //获得用户输入数据
            string UserID = this.tbUserName.Text.Trim();
            string UserPassword = this.tbPassword.Text.Trim();
            //创建与数据库的链接
            MySqlConnection conn = new MySqlConnection();
            conn.ConnectionString = "Persist Security Info=False;database=UserInfo;server=localhost;Uid=root;pwd=xian80";
            conn.Open();
            //创建对数据库查询命令
            MySqlCommand cmd = new MySqlCommand();
            cmd.Connection = conn;
            cmd.CommandType = CommandType.Text;


            cmd.CommandText = "select UserName from Users where UserID ='" + UserID + "'and Password ='" + UserPassword+"'";
            //对数据库进行查询,并将查询结果保存到数据阅读器dr中
            MySqlDataReader dr;
            dr = cmd.ExecuteReader();
            
            //判断用户是否存在
            if (count < 3)
            {
                if (dr.Read())
                {
                    MessageBox.Show(dr.GetString(0).ToString() + "欢迎你!");
                }
                else
                {
                    count++;
                    if (count < 3)
                    {
                        MessageBox.Show("用户名或密码错误,请重新输入!");
                    }
                    else
                    {
                        MessageBox.Show("您输入的次数已经超过三次,程序将关闭!");
                        this.Close();
                    }


                }
            }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值