MySQL社区

 找回密码
 注册

QQ登录

只需一步,快速开始

扫一扫,访问微社区

搜索
查看: 3780|回复: 3
打印 上一主题 下一主题

[.Net相关] MySQL执行语句重大错误

[复制链接]
跳转到指定楼层
1#
发表于 2013-1-16 14:10:31 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
使用官方提供的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.
这个问题很无厘头,请高手指导!
分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友 微信微信
收藏收藏 分享淘帖 顶1 踩
2#
发表于 2013-1-17 10:40:38 | 只看该作者
{:soso_e132:}
3#
发表于 2013-1-29 11:23:34 | 只看该作者
这是俺写的代码,一直很正常。你先看一下在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();
            }
        }
    }
4#
发表于 2013-1-29 11:25:41 | 只看该作者
连接字符串。<add name ="MYSQLSERVER" providerName="MySql.Data.MySqlClient" connectionString ="Server=10.4.9.22;Database=mobiledata;Uid=mobiledatawd=不能告诉你;"/>
您需要登录后才可以回帖 登录 | 注册

本版积分规则

QQ|申请友链|小黑屋|Archiver|手机版|MySQL社区 ( 京ICP备07012489号   
联系人:周生; 联系电话:13911732319

GMT+8, 2024-5-18 04:33 , Processed in 0.152622 second(s), 25 queries , Gzip On.

Powered by Discuz! X3.2

© 2001-2013 Comsenz Inc.

快速回复 返回顶部 返回列表