ASP.NET-C#與MySQL連接遇到問題Fatal error encountered during command execution
最近架了個MySQL Server,用mysql workbench都連得到,但用C#程式要連時,跳出以下錯誤
Fatal error encountered during command execution
後來查了一下,在connectstring中加了"Allow User Variables=True;"就解決了
連線字串可參考(config)
<add key="ConnString" value="SERVER = xxx.xx.xxx.xx; DATABASE = xxx; User ID = xxx; PASSWORD = xxxxxxxx; SslMode=none; Connect Timeout=3600;Allow User Variables=True;" />
程式後端可這樣讀string,並抓資料
public static DataTable GetData(string sql)
{
//連接字串
String conString = System.Configuration.ConfigurationManager.AppSettings["ConnString"];
//取得MySQLConnection
MySqlConnection conn = new MySqlConnection();
conn.ConnectionString = conString;
DataTable dt = new DataTable();
MySqlDataAdapter adapter = new MySqlDataAdapter(sql, conn);
adapter.Fill(dt);
return dt;
}
Fatal error encountered during command execution
後來查了一下,在connectstring中加了"Allow User Variables=True;"就解決了
連線字串可參考(config)
<add key="ConnString" value="SERVER = xxx.xx.xxx.xx; DATABASE = xxx; User ID = xxx; PASSWORD = xxxxxxxx; SslMode=none; Connect Timeout=3600;Allow User Variables=True;" />
程式後端可這樣讀string,並抓資料
public static DataTable GetData(string sql)
{
//連接字串
String conString = System.Configuration.ConfigurationManager.AppSettings["ConnString"];
//取得MySQLConnection
MySqlConnection conn = new MySqlConnection();
conn.ConnectionString = conString;
DataTable dt = new DataTable();
MySqlDataAdapter adapter = new MySqlDataAdapter(sql, conn);
adapter.Fill(dt);
return dt;
}
留言
張貼留言