日期:2014-05-16 浏览次数:20937 次
下面是别人解决问题的总结:
问题解决了。总结一下:
1.mysql数据库驱动有两种:mysql-connector-odbc和mysql-connector-net,
前者不支持参数,后者支持,这是我测试的结果;
2.参数变量使用跟SqlServer一样,只需把@变为?,
如,mysql中写为:insert into test values (?id,?title,?type,now())";
3.使用mysql-connector-net驱动很简单,到网上下载mysql-connector-net-5.2.3-src,
解压后,把mysql.data.dll添加到项目的bin文件夹即可使用;
4.连接本机mysql数据库方法例子如下---mysql-connector-net:
1)Using Web.config
<connectionstrings>
<add name="WhateverName" connectionString="server=localhost;user id=root; password=yourPassword;database=yourDatabase;;charset=utf8;"/>
</connectionstrings>
charset=utf8字符集
2)using statements
using MySql.Data.MySqlClient;
下面附上测试通过的代码:
string connStr = WebConfigurationManager.
ConnectionStrings["WhateverName"].ConnectionString;
MySqlConnection conn = new MySqlConnection(connStr);
if (conn != null)
conn.Open();
else
return;
string SQL_INSERT_TOPIC = "insert into test values (null,?title,?type,now())";
string PARM_TITLE = "?title";
string PARM_TYPE = "?type";
MySqlParameter[] parms = new MySqlParameter[]
{
new MySqlParameter(PARM_TITLE,MySqlDbType.VarChar,80),
new MySqlParameter(PARM_TYPE,MySqlDbType.VarChar,1)
};
parms[0].Value = "welcome to beijing";
parms[1].Value = "C";
MySqlCommand cmd = new MySqlCommand();
cmd.Connection = conn;
cmd.CommandType = CommandType.Text;
cmd.CommandText = SQL_INSERT_TOPIC;
foreach (MySqlParameter pram in parms)
cmd.Parameters.Add(pram);
cmd.ExecuteNonQuery();
conn.Close();
5.连接本机mysql数据库方法例子如---mysql-connector-odbc
?
转自:
http://dotnetperls.com/Content/OdbcConnection-Example.aspx