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

相关文章

SQL server数据库如何下载和安装

《SQLserver数据库如何下载和安装》本文指导如何下载安装SQLServer2022评估版及SSMS工具,涵盖安装配置、连接字符串设置、C#连接数据库方法和安全注意事项,如混合验证、参数化查... 目录第一步:打开官网下载对应文件第二步:程序安装配置第三部:安装工具SQL Server Manageme

C#连接SQL server数据库命令的基本步骤

《C#连接SQLserver数据库命令的基本步骤》文章讲解了连接SQLServer数据库的步骤,包括引入命名空间、构建连接字符串、使用SqlConnection和SqlCommand执行SQL操作,... 目录建议配合使用:如何下载和安装SQL server数据库-CSDN博客1. 引入必要的命名空间2.

全面掌握 SQL 中的 DATEDIFF函数及用法最佳实践

《全面掌握SQL中的DATEDIFF函数及用法最佳实践》本文解析DATEDIFF在不同数据库中的差异,强调其边界计算原理,探讨应用场景及陷阱,推荐根据需求选择TIMESTAMPDIFF或inte... 目录1. 核心概念:DATEDIFF 究竟在计算什么?2. 主流数据库中的 DATEDIFF 实现2.1

MySQL 多列 IN 查询之语法、性能与实战技巧(最新整理)

《MySQL多列IN查询之语法、性能与实战技巧(最新整理)》本文详解MySQL多列IN查询,对比传统OR写法,强调其简洁高效,适合批量匹配复合键,通过联合索引、分批次优化提升性能,兼容多种数据库... 目录一、基础语法:多列 IN 的两种写法1. 直接值列表2. 子查询二、对比传统 OR 的写法三、性能分析

MySQL中的LENGTH()函数用法详解与实例分析

《MySQL中的LENGTH()函数用法详解与实例分析》MySQLLENGTH()函数用于计算字符串的字节长度,区别于CHAR_LENGTH()的字符长度,适用于多字节字符集(如UTF-8)的数据验证... 目录1. LENGTH()函数的基本语法2. LENGTH()函数的返回值2.1 示例1:计算字符串

浅谈mysql的not exists走不走索引

《浅谈mysql的notexists走不走索引》在MySQL中,​NOTEXISTS子句是否使用索引取决于子查询中关联字段是否建立了合适的索引,下面就来介绍一下mysql的notexists走不走索... 在mysql中,​NOT EXISTS子句是否使用索引取决于子查询中关联字段是否建立了合适的索引。以下

Java通过驱动包(jar包)连接MySQL数据库的步骤总结及验证方式

《Java通过驱动包(jar包)连接MySQL数据库的步骤总结及验证方式》本文详细介绍如何使用Java通过JDBC连接MySQL数据库,包括下载驱动、配置Eclipse环境、检测数据库连接等关键步骤,... 目录一、下载驱动包二、放jar包三、检测数据库连接JavaJava 如何使用 JDBC 连接 mys

SQL中如何添加数据(常见方法及示例)

《SQL中如何添加数据(常见方法及示例)》SQL全称为StructuredQueryLanguage,是一种用于管理关系数据库的标准编程语言,下面给大家介绍SQL中如何添加数据,感兴趣的朋友一起看看吧... 目录在mysql中,有多种方法可以添加数据。以下是一些常见的方法及其示例。1. 使用INSERT I

Qt使用QSqlDatabase连接MySQL实现增删改查功能

《Qt使用QSqlDatabase连接MySQL实现增删改查功能》这篇文章主要为大家详细介绍了Qt如何使用QSqlDatabase连接MySQL实现增删改查功能,文中的示例代码讲解详细,感兴趣的小伙伴... 目录一、创建数据表二、连接mysql数据库三、封装成一个完整的轻量级 ORM 风格类3.1 表结构

MySQL 中的 CAST 函数详解及常见用法

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