我使用MySql数据库,我用C#编写了给一个有数据的表添加一个字段,添加不进去,而且老是报异常。
Sql语句是:ALTER TABLE patroldata ADD COLUMN  SoundRec MEDIUMBLOB  ,给patroldata表中添加一个名为SoundRec的字段。
Sql的语句执行函数是:
 public bool Execute(string sentence, out ArrayList resultList)//sentence是Sql语句
        {
             resultList = new ArrayList();
             
            try
            {
                if (!BeforeCommandExecute(sentence))//连接数据库
                    return false;                reader = command.ExecuteReader();                while (reader.Read())
                {
                    ArrayList itemList = new ArrayList();
                    for (int i = 0; i < reader.FieldCount;i++ )
                        itemList.Add(reader.GetValue(i));
                    resultList.Add(itemList);
                    
                }
               
            }
            catch (MySqlException se)
            {
                conn.Close();
                string errorInfo = string.Format("语句{0}执行有误!",sentence);
                MessageBox.Show(errorInfo, "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return false;
            }           AfterCommandExcute();//关闭数据库连接
            return true;
        }

解决方案 »

  1.   

    在MYSQL中测试,没有问题啊,提示什么?
      

  2.   

    异常是:Fatal Error encounted during Command execution.
    当表的数据相当庞大的时候,增加一个字段就增加不进去,主要是MySql 增加一个字段,就是在每一条记录的后面扩出一个位置来,记录多了,速度就慢,记录少时,没有问题,记录多了就出问题,因此在执行 reader = command.ExecuteReader();这条语句时,因为执行超时而报异常。请哪位高手帮着解决一下。
      

  3.   

    alter table  会重新生成一张表,然后再将旧表删除,所以如果数据很多,表复制会需要比较和时间,很容易造成连接,或查询超时。
    检查你的时间设置。
    mysql> show variables like '%timeout%';
    +----------------------------+-------+
    | Variable_name              | Value |
    +----------------------------+-------+
    | connect_timeout            | 10    |
    | delayed_insert_timeout     | 300   |
    | innodb_lock_wait_timeout   | 50    |
    | innodb_rollback_on_timeout | OFF   |
    | interactive_timeout        | 28800 |
    | net_read_timeout           | 30    |
    | net_write_timeout          | 60    |
    | slave_net_timeout          | 3600  |
    | table_lock_wait_timeout    | 50    |
    | wait_timeout               | 28800 |
    +----------------------------+-------+
    10 rows in set (0.00 sec)
      

  4.   

    if (!BeforeCommandExecute(sentence))//连接数据库这行到底是干什么的?不是连接数据库吧,更改表结构?
    把它的实现帖出来吧。
    还有,异常信息也可以打出来啊。这么有用的东西不用
      

  5.   


     private bool BeforeCommandExecute(string sentence)
            {
                try 
                {                
                    command = conn.CreateCommand();
                    command.CommandText = sentence;
                    conn.Open();
                    return true;
                }
                catch (MySqlException se)
                {
                    conn.Close();
                    MessageBox.Show("数据库连接不上!","提示",MessageBoxButtons.OK,MessageBoxIcon.Information);                 
                    return false;
                }
                
            }
    异常信息是:Fatal Error encounted during Command execution.
      

  6.   

    command.ExecuteReader() 改为executeNonQuery()如何?当然这可能不是致命原因。另外,可能需要修改几个timeout参数,将其变大。