VS2010与mysql

2024-08-28 04:38
文章标签 mysql database vs2010

本文主要是介绍VS2010与mysql,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

一、C#读取mysql乱码
(1)连接mysql时设置charset:
            MySql.Data.MySqlClient.MySqlConnection conn;
            MySqlCommand myCommand = new MySqlCommand();
            string myConnectionString = "server=127.0.0.1; uid = root; pwd=;database=weibo; charset=utf8;";
            conn = new MySql.Data.MySqlClient.MySqlConnection(myConnectionString);
(2)创建数据库时,设置字符集:

            CREATE DATABASE db_name    CHARACTER SET utf8;

二、C存入到mysql乱码
连接mysql室设置字符集:
            MYSQL my_connection;
             int res;
             if (mysql_real_connect(&my_connection, "localhost", "root", "", "weibo", 0, NULL, 0)) {
                  if (!mysql_set_character_set(&my_connection, "utf8"))
                  {
                       cout << "Set character error " << mysql_character_set_name(&my_connection) << endl;
                 
 }
                  cout << "Connection DB success\n" << endl;
                //其他代码
            }

三、C#连接mysql
简要过程:1、下载MySQL Connector/NET: http://dev.mysql.com/downloads/connector/net/1.0.html。
                   2、在Vs2010项目中,解决方案处点击“引用”-“添加引用”,在“.NET “下选择“Mysql.Data”。之后就可以在C#中编写操作Mysql数据库的代码了。




-----------------------------------------------------------分割线--------------------------------------------------------------------------------
以下转载
完整解决过程: http://www.codeproject.com/Articles/21919/Connecting-to-MySQL-from-Visual-Csharp

Connecting to the Database from C#

What will allow us to work with the data in any MySQL database from C# is a reference to the MySql.Data assembly, which is registered into the Global Assembly Cache after the MySQL Connector/NET installation. First, create a new Console Application project in Visual C#. You may call it MySQLDBConnection or whatever name you decide. Now in the Solution Explorer within the Visual C# IDE, right click on the References folder and choose Add Reference... as shown below:

In the Add Reference dialog box that appears, select the MySQL.Data item from the list:

Now, after doing that, we must add the using MySql.Data.MySqlClient statement to our code in the Visual C# IDE:

Below you will find the basic lines of code needed to perform the connection to any MySQL database from C#:

using System;
using System.Data;
using System.Data.Common;
using System.Collections.Generic;
using System.Text;
using MySql.Data.MySqlClient;namespace MySQLDBConnection
{class Program{static void Main(string[] args){MySqlConnectionStringBuilder connBuilder = new MySqlConnectionStringBuilder();connBuilder.Add("Database", "shop");connBuilder.Add("Data Source", "localhost");connBuilder.Add("User Id", "root");connBuilder.Add("Password", "masterkey");MySqlConnection connection = new MySqlConnection(connBuilder.ConnectionString);MySqlCommand cmd = connection.CreateCommand();connection.Open();// Here goes the code needed to perform operations on the
            // database such as querying or inserting rows into a table
connection.Close();}}
}

Notice that I've decided to use the MySqlConnectionStringBuilder class instead of putting all the connection items into a single string. This promotes better readability and maintainability of your code.

First, we start by adding a reference to the MySql.Data.MySqlClient namespace in our code. Then we create a MySqlConnectionStringBuilder instance and we add pairs of name/value items for the database name, data source, user ID and password. After that, we create an instance of the MySqlConnection class and we pass the ConnectionString from our MySqlConnectionStringBuilder instance as a parameter.

Let's create the following two methods within the Program class. One is for reading the contents of the table we are working with and the other is for appending new data into it.

public static void QueryCommand(MySqlCommand cmd)
{cmd.CommandText = "SELECT * FROM article";cmd.CommandType = CommandType.Text;MySqlDataReader reader = cmd.ExecuteReader();while (reader.Read()){Console.WriteLine(String.Format("{0}, {1}, {2}",reader.GetInt32(0), reader.GetString(1), reader.GetDouble(2)));}reader.Close();
}public static void InsertCommand(MySqlCommand cmd, string name, double price)
{cmd.CommandText = "append_data";cmd.CommandType = CommandType.StoredProcedure;cmd.Parameters.Add(new MySqlParameter("param_name", name));cmd.Parameters.Add(new MySqlParameter("param_price", price));cmd.ExecuteNonQuery();
}

Now let's add some code between the connection.Open() and connection.Close() statements to perform some basic operations on the database.

InsertCommand(cmd, "MQ95 Flat Monitor", 399.00);
QueryCommand(cmd);

这篇关于VS2010与mysql的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1113751

相关文章

MySQL 中的 JSON 查询案例详解

《MySQL中的JSON查询案例详解》:本文主要介绍MySQL的JSON查询的相关知识,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考下吧... 目录mysql 的 jsON 路径格式基本结构路径组件详解特殊语法元素实际示例简单路径复杂路径简写操作符注意MySQL 的 J

Windows 上如果忘记了 MySQL 密码 重置密码的两种方法

《Windows上如果忘记了MySQL密码重置密码的两种方法》:本文主要介绍Windows上如果忘记了MySQL密码重置密码的两种方法,本文通过两种方法结合实例代码给大家介绍的非常详细,感... 目录方法 1:以跳过权限验证模式启动 mysql 并重置密码方法 2:使用 my.ini 文件的临时配置在 Wi

MySQL重复数据处理的七种高效方法

《MySQL重复数据处理的七种高效方法》你是不是也曾遇到过这样的烦恼:明明系统测试时一切正常,上线后却频频出现重复数据,大批量导数据时,总有那么几条不听话的记录导致整个事务莫名回滚,今天,我就跟大家分... 目录1. 重复数据插入问题分析1.1 问题本质1.2 常见场景图2. 基础解决方案:使用异常捕获3.

SQL中redo log 刷⼊磁盘的常见方法

《SQL中redolog刷⼊磁盘的常见方法》本文主要介绍了SQL中redolog刷⼊磁盘的常见方法,将redolog刷入磁盘的方法确保了数据的持久性和一致性,下面就来具体介绍一下,感兴趣的可以了解... 目录Redo Log 刷入磁盘的方法Redo Log 刷入磁盘的过程代码示例(伪代码)在数据库系统中,r

mysql中的group by高级用法

《mysql中的groupby高级用法》MySQL中的GROUPBY是数据聚合分析的核心功能,主要用于将结果集按指定列分组,并结合聚合函数进行统计计算,下面给大家介绍mysql中的groupby用法... 目录一、基本语法与核心功能二、基础用法示例1. 单列分组统计2. 多列组合分组3. 与WHERE结合使

Mysql用户授权(GRANT)语法及示例解读

《Mysql用户授权(GRANT)语法及示例解读》:本文主要介绍Mysql用户授权(GRANT)语法及示例,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录mysql用户授权(GRANT)语法授予用户权限语法GRANT语句中的<权限类型>的使用WITH GRANT

Mysql如何解决死锁问题

《Mysql如何解决死锁问题》:本文主要介绍Mysql如何解决死锁问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录【一】mysql中锁分类和加锁情况【1】按锁的粒度分类全局锁表级锁行级锁【2】按锁的模式分类【二】加锁方式的影响因素【三】Mysql的死锁情况【1

SQL BETWEEN 的常见用法小结

《SQLBETWEEN的常见用法小结》BETWEEN操作符是SQL中非常有用的工具,它允许你快速选取某个范围内的值,本文给大家介绍SQLBETWEEN的常见用法,感兴趣的朋友一起看看吧... 在SQL中,BETWEEN是一个操作符,用于选取介于两个值之间的数据。它包含这两个边界值。BETWEEN操作符常用

MySQL索引的优化之LIKE模糊查询功能实现

《MySQL索引的优化之LIKE模糊查询功能实现》:本文主要介绍MySQL索引的优化之LIKE模糊查询功能实现,本文通过示例代码给大家介绍的非常详细,感兴趣的朋友一起看看吧... 目录一、前缀匹配优化二、后缀匹配优化三、中间匹配优化四、覆盖索引优化五、减少查询范围六、避免通配符开头七、使用外部搜索引擎八、分

MySql match against工具详细用法

《MySqlmatchagainst工具详细用法》在MySQL中,MATCH……AGAINST是全文索引(Full-Textindex)的查询语法,它允许你对文本进行高效的全文搜素,支持自然语言搜... 目录一、全文索引的基本概念二、创建全文索引三、自然语言搜索四、布尔搜索五、相关性排序六、全文索引的限制七