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 中的 CAST 函数详解及常见用法

《MySQL中的CAST函数详解及常见用法》CAST函数是MySQL中用于数据类型转换的重要函数,它允许你将一个值从一种数据类型转换为另一种数据类型,本文给大家介绍MySQL中的CAST... 目录mysql 中的 CAST 函数详解一、基本语法二、支持的数据类型三、常见用法示例1. 字符串转数字2. 数字

Mysql实现范围分区表(新增、删除、重组、查看)

《Mysql实现范围分区表(新增、删除、重组、查看)》MySQL分区表的四种类型(范围、哈希、列表、键值),主要介绍了范围分区的创建、查询、添加、删除及重组织操作,具有一定的参考价值,感兴趣的可以了解... 目录一、mysql分区表分类二、范围分区(Range Partitioning1、新建分区表:2、分

MySQL 定时新增分区的实现示例

《MySQL定时新增分区的实现示例》本文主要介绍了通过存储过程和定时任务实现MySQL分区的自动创建,解决大数据量下手动维护的繁琐问题,具有一定的参考价值,感兴趣的可以了解一下... mysql创建好分区之后,有时候会需要自动创建分区。比如,一些表数据量非常大,有些数据是热点数据,按照日期分区MululbU

SQL Server配置管理器无法打开的四种解决方法

《SQLServer配置管理器无法打开的四种解决方法》本文总结了SQLServer配置管理器无法打开的四种解决方法,文中通过图文示例介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的... 目录方法一:桌面图标进入方法二:运行窗口进入检查版本号对照表php方法三:查找文件路径方法四:检查 S

MySQL 删除数据详解(最新整理)

《MySQL删除数据详解(最新整理)》:本文主要介绍MySQL删除数据的相关知识,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考下吧... 目录一、前言二、mysql 中的三种删除方式1.DELETE语句✅ 基本语法: 示例:2.TRUNCATE语句✅ 基本语

MySQL中查找重复值的实现

《MySQL中查找重复值的实现》查找重复值是一项常见需求,比如在数据清理、数据分析、数据质量检查等场景下,我们常常需要找出表中某列或多列的重复值,具有一定的参考价值,感兴趣的可以了解一下... 目录技术背景实现步骤方法一:使用GROUP BY和HAVING子句方法二:仅返回重复值方法三:返回完整记录方法四:

从入门到精通MySQL联合查询

《从入门到精通MySQL联合查询》:本文主要介绍从入门到精通MySQL联合查询,本文通过实例代码给大家介绍的非常详细,需要的朋友可以参考下... 目录摘要1. 多表联合查询时mysql内部原理2. 内连接3. 外连接4. 自连接5. 子查询6. 合并查询7. 插入查询结果摘要前面我们学习了数据库设计时要满

MySQL查询JSON数组字段包含特定字符串的方法

《MySQL查询JSON数组字段包含特定字符串的方法》在MySQL数据库中,当某个字段存储的是JSON数组,需要查询数组中包含特定字符串的记录时传统的LIKE语句无法直接使用,下面小编就为大家介绍两种... 目录问题背景解决方案对比1. 精确匹配方案(推荐)2. 模糊匹配方案参数化查询示例使用场景建议性能优

mysql表操作与查询功能详解

《mysql表操作与查询功能详解》本文系统讲解MySQL表操作与查询,涵盖创建、修改、复制表语法,基本查询结构及WHERE、GROUPBY等子句,本文结合实例代码给大家介绍的非常详细,感兴趣的朋友跟随... 目录01.表的操作1.1表操作概览1.2创建表1.3修改表1.4复制表02.基本查询操作2.1 SE

MySQL中的锁机制详解之全局锁,表级锁,行级锁

《MySQL中的锁机制详解之全局锁,表级锁,行级锁》MySQL锁机制通过全局、表级、行级锁控制并发,保障数据一致性与隔离性,全局锁适用于全库备份,表级锁适合读多写少场景,行级锁(InnoDB)实现高并... 目录一、锁机制基础:从并发问题到锁分类1.1 并发访问的三大问题1.2 锁的核心作用1.3 锁粒度分