CSharp直接连接MySQL
来源:互联网 发布:js算法题 编辑:程序博客网 时间:2024/06/06 19:04
以下是测试源码:
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;using MySql.Data.MySqlClient;namespace CSharp直接连接MySQL{ class Program { static void Main(string[] args) { //Insert(); //Update(); // Delete(); //Read(); //ReadUserCount(); // ExcuteScalar(); Console.WriteLine( VerifyUser("c", "d")); Console.ReadKey(); } static bool VerifyUser(string username,string password) //通过这个验证mysql中的数据库中是否存在账号密码 { string connectStr = "server=127.0.0.1;port=3306;database=mygamedb;user=root;password=root";//填写用来连接数据库的IP地址,用户名密码 ,和连接的数据库 MySqlConnection conn = new MySqlConnection(connectStr); // 建立连接通道 并没有跟数据库跟数据建立连接 try { conn.Open(); //string sql = "select * from users where username='"+ username + "'and password='"+ password + "'"; //我们自己按照查询条件组拼mysql 很麻烦 string sql = "select * from users where username=@username and password=@password "; MySqlCommand command = new MySqlCommand(sql, conn); //如何向MySQL发起命令 command.Parameters.AddWithValue("username", username); command.Parameters.AddWithValue("password", password); MySqlDataReader reader = command.ExecuteReader(); if (reader.Read()) { return true; } } catch (Exception e) { Console.WriteLine(e.ToString()); } finally { conn.Close(); } return false; } // static void Read() { string connectStr = "server=127.0.0.1;port=3306;database=mygamedb;user=root;password=root";//填写用来连接数据库的IP地址,用户名密码 ,和连接的数据库 MySqlConnection conn = new MySqlConnection(connectStr); // 建立连接通道 并没有跟数据库跟数据建立连接 try { conn.Open(); //string sql = "select id , password from users"; string sql= "select * from users"; MySqlCommand command = new MySqlCommand(sql, conn); //如何向MySQL发起命令 MySqlDataReader reader = command.ExecuteReader(); //reader.Read(); //调用一次相当于翻一页书 //Console.WriteLine(reader[0].ToString() + reader[1].ToString() + reader[2].ToString()); while (reader.Read()) //如果有数据 返回True 没数据返回false { //Console.WriteLine(reader[0].ToString() + reader[1].ToString() /*+ reader[2].ToString()*/); //Console.WriteLine( reader.GetInt32(0)+ " "+reader.GetString(1)+ " "+reader.GetString(2)); Console.WriteLine( reader.GetInt32("id")+ " "+reader.GetString("username")+ " "+reader.GetString("password")+""+reader[3].ToString()); } //command.ExecuteReader(); //执行一些查询 //command.ExecuteNonQuery(); //插入删除 //command.ExecuteScalar();//执行一些查询,返回一个单个值 Console.WriteLine("已经建立连接"); } catch (Exception e) { Console.WriteLine(e.ToString()); } finally { conn.Close(); } Console.ReadKey(); } static void Insert() { string connectStr = "server=127.0.0.1;port=3306;database=mygamedb;user=root;password=root";//填写用来连接数据库的IP地址,用户名密码 ,和连接的数据库 MySqlConnection conn = new MySqlConnection(connectStr); // 建立连接通道 并没有跟数据库跟数据建立连接 try { conn.Open(); // string sql = "insert into users(username,password) values('f','g')"; string sql = "insert into users(username,password,time) values('f','g','"+DateTime.Now+"')"; MySqlCommand command = new MySqlCommand(sql, conn); //如何向MySQL发起命令 // MySqlDataReader reader = command.ExecuteReader(); int result= command.ExecuteNonQuery(); //返回的是数据库中受影响的数据的行数 Console.WriteLine(result); //Console.WriteLine("已经建立连接"); } catch (Exception e) { Console.WriteLine(e.ToString()); } finally { conn.Close(); } Console.ReadKey(); } static void Update() { string connectStr = "server=127.0.0.1;port=3306;database=mygamedb;user=root;password=root";//填写用来连接数据库的IP地址,用户名密码 ,和连接的数据库 MySqlConnection conn = new MySqlConnection(connectStr); // 建立连接通道 并没有跟数据库跟数据建立连接 try { conn.Open(); string sql = "update users set username='abc',password='222' where id='3'"; MySqlCommand command = new MySqlCommand(sql, conn); //如何向MySQL发起命令 // MySqlDataReader reader = command.ExecuteReader(); int result = command.ExecuteNonQuery(); //返回的是数据库中受影响的数据的行数 Console.WriteLine(result); //Console.WriteLine("已经建立连接"); } catch (Exception e) { Console.WriteLine(e.ToString()); } finally { conn.Close(); } Console.ReadKey(); } static void Delete() { string connectStr = "server=127.0.0.1;port=3306;database=mygamedb;user=root;password=root";//填写用来连接数据库的IP地址,用户名密码 ,和连接的数据库 MySqlConnection conn = new MySqlConnection(connectStr); // 建立连接通道 并没有跟数据库跟数据建立连接 try { conn.Open(); string sql = "delete from users where id='3'"; MySqlCommand command = new MySqlCommand(sql, conn); //如何向MySQL发起命令 // MySqlDataReader reader = command.ExecuteReader(); int result = command.ExecuteNonQuery(); //返回的是数据库中受影响的数据的行数 Console.WriteLine(result); //Console.WriteLine("已经建立连接"); } catch (Exception e) { Console.WriteLine(e.ToString()); } finally { conn.Close(); } Console.ReadKey(); } static void ReadUserCount() { string connectStr = "server=127.0.0.1;port=3306;database=mygamedb;user=root;password=root";//填写用来连接数据库的IP地址,用户名密码 ,和连接的数据库 MySqlConnection conn = new MySqlConnection(connectStr); // 建立连接通道 并没有跟数据库跟数据建立连接 try { conn.Open(); //string sql = "select id , password from users"; string sql = "select count(*) from users"; MySqlCommand command = new MySqlCommand(sql, conn); //如何向MySQL发起命令 MySqlDataReader reader = command.ExecuteReader(); reader.Read(); int count = Convert.ToInt32(reader[0].ToString()); Console.WriteLine(count); Console.WriteLine("已经建立连接"); } catch (Exception e) { Console.WriteLine(e.ToString()); } finally { conn.Close(); } Console.ReadKey(); } static void ExcuteScalar() //当返回值只有一个值的时候 使用ExcuteScalar 比较方便 { string connectStr = "server=127.0.0.1;port=3306;database=mygamedb;user=root;password=root";//填写用来连接数据库的IP地址,用户名密码 ,和连接的数据库 MySqlConnection conn = new MySqlConnection(connectStr); // 建立连接通道 并没有跟数据库跟数据建立连接 try { conn.Open(); //string sql = "select id , password from users"; string sql = "select count(*) from users"; MySqlCommand command = new MySqlCommand(sql, conn); //如何向MySQL发起命令 object reader = command.ExecuteScalar(); int count = Convert.ToInt32(reader.ToString()); Console.WriteLine(count); Console.WriteLine("已经建立连接"); } catch (Exception e) { Console.WriteLine(e.ToString()); } finally { conn.Close(); } Console.ReadKey(); } }}
阅读全文
0 0
- CSharp直接连接MySQL
- java直接连接mysql数据库
- android不能直接连接mysql
- C#与MySQL直接连接
- Csharp和SQL连接
- Csharp连接Access数据库
- otl 直接使用字符串连接mysql数据库
- Delphi ADO MySQL直接连接方式
- otl 直接使用字符串连接mysql数据库
- otl 直接使用字符串连接mysql数据库
- Java:直接连接MySQL的相关
- MapReduce直接连接Mysql获取数据
- android 通过jdbc直接连接mysql
- MapReduce直接连接MySQL获取数据
- CSharp 使用ADO连接 SQLServer2008
- csharp
- Csharp
- csharp tcp/ip连接发送消息
- 快速傅立叶变换(FFT)
- 欢迎使用CSDN-markdown编辑器
- Problem-1248 寒冰王座
- 【BZOJ】1212 [HNOI2004]L语言 Trie
- pxe全自动化安装虚拟机
- CSharp直接连接MySQL
- 为什么C++的SEH不提供finally
- Hexo博客添加SEO-评论系统-阅读统计-站长统计
- openGL ES进阶教程(五)制作一个简单的VR播放器,播放全景视频
- sharedpreference入门
- 推荐给大家一个优惠卷搜索网站
- java分布式服务框架Dubbo的介绍与使用
- amazeui表单提交实例
- hadoop ha配置后一个namenode不能自动failover,相当于没有配置ha