VS2010与mysql
来源:互联网 发布:mac运行32位软件 编辑:程序博客网 时间:2024/06/07 13:41
(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;
//其他代码
}
简要过程: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 chooseAdd 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 aMySqlConnectionStringBuilder
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 theMySqlConnection
class and we pass the ConnectionString
from ourMySqlConnectionStringBuilder
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
- vs2010与mysql数据库中文乱码问题!
- Win7+VS2010:mysql 源码编译与调试
- [EF在VS2010中应用Entity framework与MySQL
- [EF在VS2010中应用Entity framework与MySQL
- MySql与VS2010 C#连接方法之ODBC
- C#连接mysql (vs2010)
- C#连接mysql (vs2010)
- vs2010连接mysql数据库
- vs2010连接Mysql数据库
- VS2010 连接MySQL
- vs2010连接 mysql
- VS2010连接mysql数据库
- vs2010连接mysql
- C#连接mysql (vs2010)
- vs2010 c++ 链接mysql
- vs2010 连接MySQL
- VS2010连接MySQL数据库
- 在vs2010下编译boost
- 多线程执行多类型任务(线程通信wait/notify(notifyAll)):生产者消费者模型(二)
- spring中使用logback日志组件替换log4j
- Android中判断输入的字符串是否为数字,可以解决如输入为点出现的问题
- Android最新组件RecyclerView,替代ListView
- VS2010与mysql
- STL源码之实现一个简易的Vector容器
- Dongle烧写模块重构(四)--用工厂模式将方案商从功能代码中抽离
- servlet 学习二
- OllyDbg破解系列学习笔记(四)
- UVA 10201 Adventures in Moving - Part IV(动态规划)
- 检测TCP端口占用状态的函数
- ASP.NET MVC3开发 - CodeFisrt数据库篇之M层验证之调用远程方法(Remote)验证
- 用fork函数,由一个父进程生成两个子进程