MariaDB社区
标题: MySQL执行语句重大错误 [打印本页]
作者: TieMushan 时间: 2013-1-16 14:10
标题: MySQL执行语句重大错误
使用官方提供的MySQL-Connector-.net 6.6.4连接器,创建如下代码:
① string connstr = "server=localhost;database=test;uid=root;password= 123";
② MySqlConnection mysqlConn = new MySqlConnection(connstr);
③ mysqlConn.Open();
④ string commandstr = "set @param = '14';"
⑤ MySqlCommand mysqlcommand = new MySqlCommand(commandstr, mysqlConn);
⑥ mysqlcommand.ExecuteNonQuery();
当执行到语句⑥时,报错:Fatal error encountered during command execution.
这个问题很无厘头,请高手指导!
作者: kider 时间: 2013-1-17 10:40
作者: 末猪 时间: 2013-1-29 11:23
这是俺写的代码,一直很正常。你先看一下在open的时候是否打开,先try一下;
public class database
{
//数据库连接字符串
private string DB_LINK_STR = ConfigurationManager.ConnectionStrings["SQLSERVER"].ToString();
//连接对象
private SqlConnection sqlconn = new SqlConnection();
//脚本执行对象
private SqlCommand sqlcom = new SqlCommand();
//执行成功记录
private bool _isFail = true;
/*
* 执行脚本
*/
public bool ExceSQL(string cmd) {
openDB();
command(cmd);
DBClose();
return _isFail;
}
/*
* 返回异常指标
*/
public XmlDocument returnYCindex() {
openDB();
sqlcom.Connection = sqlconn;
sqlcom.CommandText = "select "+
"[业务名称] as 'index',"+
"convert(varchar(10),convert(float(2),[用户稽核比例])) as 'bili', " +
"[异常指标] as 'yichangindex',"+
"[超标地市(10%以上)] as 'chao10',"+
"[超标地市(8%--10%)] as 'chao0810',"+
"[未超标地市(< 8%)] as 'weichao'"+
" from v_jihe"+
" order by [用户稽核比例] desc;";
sqlcom.CommandType = System.Data.CommandType.Text;
SqlDataReader reader = sqlcom.ExecuteReader(System.Data.CommandBehavior.CloseConnection);
XmlDocument xmlDoc = new XmlDocument();
XmlDeclaration xmlDec = xmlDoc.CreateXmlDeclaration("1.0", "gb2312", null);
xmlDoc.AppendChild(xmlDec);
XmlNode rootNode = xmlDoc.CreateElement("allIndex");
XmlNode index; //
XmlAttribute indexName;//指标名
XmlAttribute jihebili;//稽核比例
XmlAttribute yichangindex;//异常指标
XmlAttribute chao10; //超10%
XmlAttribute chao0810;//区域8%-10%
XmlAttribute weichao;//未超标
while (reader.Read())
{
index = xmlDoc.CreateElement("index");
indexName= xmlDoc.CreateAttribute("indexName");
indexName.Value = reader["index"].ToString();
jihebili = xmlDoc.CreateAttribute("bili");
jihebili.Value = reader["bili"].ToString();
yichangindex = xmlDoc.CreateAttribute("ycindex");
yichangindex.Value = reader["yichangindex"].ToString();
chao10 = xmlDoc.CreateAttribute("chao10");
chao10.Value = reader["chao10"].ToString();
chao0810 = xmlDoc.CreateAttribute("chao0810");
chao0810.Value = reader["chao0810"].ToString();
weichao = xmlDoc.CreateAttribute("weichao");
weichao.Value = reader["weichao"].ToString();
index.Attributes.Append(indexName);
index.Attributes.Append(jihebili);
index.Attributes.Append(yichangindex);
index.Attributes.Append(chao10);
index.Attributes.Append(chao0810);
index.Attributes.Append(weichao);
rootNode.AppendChild(index);
}
xmlDoc.AppendChild(rootNode);
return xmlDoc;
}
/*
* 打开数据库连接
*/
private void openDB() {
sqlconn.ConnectionString = DB_LINK_STR;
try
{
sqlconn.Open();
}
catch (SqlException)
{
//log4net XML
}
}
/// <summary>
/// 脚本执行对象
/// </summary>
private void command(string cmd) {
sqlcom.Connection = sqlconn;
sqlcom.CommandText = cmd;
sqlcom.CommandType = System.Data.CommandType.Text;
try
{
if (sqlcom.ExecuteNonQuery() > 0)
{
_isFail = false;
}
}
catch (SqlException)
{
//log4net XML
}
}
/// <summary>
/// 关闭数据库及其脚本执行对象
/// </summary>
private void DBClose() {
sqlcom.Dispose();
if (sqlconn.State == System.Data.ConnectionState.Open)
{
sqlconn.Close();
sqlconn.Dispose();
}
}
}
作者: 末猪 时间: 2013-1-29 11:25
连接字符串。<add name ="MYSQLSERVER" providerName="MySql.Data.MySqlClient" connectionString ="Server=10.4.9.22;Database=mobiledata;Uid=mobiledatawd=不能告诉你;"/>
欢迎光临 MariaDB社区 (http://123.56.88.72/) |
Powered by Discuz! X3.2 |