C#操作mysql问题

cdzy12154 2011-09-10 08:32:52
string myConnstr = "Server=localhost:3306;User ID=root;Password=;Database=test;CharSet=utf8;";
MySQLConnection conn = null;
conn = new MySQLConnection(myConnstr);
conn.Open();
MySQLCommand commn1 = new MySQLCommand();
commn1.CommandText = sqlstr;
commn1.ExecuteNonQuery();
conn.Close();


报错:MySQLDriverCS Exception: Connection must be valid and open.

大侠告诉我下怎么解决 谢谢



我之前用的是
conn = new MySQLConnection(new MySQLConnectionString("localhost", "test", "root","").AsString);
MySQLCommand commn = new MySQLCommand("set names utf8", conn);
但是插入进去的是乱码
我的mysql是utf8 的






...全文
187 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
cdzy12154 2011-09-11
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 jzf2050 的回复:]
可能是连接字符串的问题,你试下下面的:
Server=myServerAddress;Port=1234;Database=myDataBase;Uid=myUsername;Pwd=myPassword;
[/Quote]


ySQLConnection conn = new MySQLConnection("Server=localhost;Port=3306;Database=test;Uid=root;Pwd=;");
open 失败



错误:
MySQLDriverCS Exception: MySQLDriverCS Error: can't connect.Access denied for user 'ODBC'@'localhost' (using password: NO)
cdzy12154 2011-09-11
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 jzf2050 的回复:]
charset=utf-8
不是
charset=utf8
[/Quote]
还是乱码
conn = new MySQLConnection("Data Source=dedecmsv57utf8;Password=;User ID=root;Location=localhost;Charset=utf-8;");
山东蓝鸟贵薪 2011-09-11
  • 打赏
  • 举报
回复
建议你跟踪测试一下吧,可能是参数在传递时就有问题的
Lemon2050 2011-09-11
  • 打赏
  • 举报
回复
charset=utf-8
不是
charset=utf8
Lemon2050 2011-09-11
  • 打赏
  • 举报
回复
可能是连接字符串的问题,你试下下面的:
Server=myServerAddress;Port=1234;Database=myDataBase;Uid=myUsername;Pwd=myPassword;
cdzy12154 2011-09-11
  • 打赏
  • 举报
回复
MySQLConnection conn = null;
conn = new MySQLConnection("Data Source=test;Password=;User ID=root;Location=localhost;CharSet=utf8");
conn.Open();
MySQLCommand cmd = new MySQLCommand("insert into addonarticle(body) values(\"测试\")", conn);
//cmd.CommandText = "";
cmd.ExecuteNonQuery();
conn.Close();


数据库的mysql 是utf8的,



CharSet=utf8和CharSet=GBK 都试了 乱码
用的是APMServ 5.26 里面集成的mysql


服务器版本: 5.1.28-rc-community
Protocol version: 10
服务器: 127.0.0.1 via TCP/IP
用户: root@localhost
MySQL 字符集: UTF-8 Unicode (utf8)
MySQL client version: 5.0.51a
Used PHP extensions: mysql
cjh200102 2011-09-11
  • 打赏
  • 举报
回复
按楼上的先看下,应该是编码格式不一致
threenewbee 2011-09-10
  • 打赏
  • 举报
回复
new MySQLConnectionString("localhost", "test", "root","").AsString
看看这个输出什么。

110,537

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧