使用C#的OleDb中的InvalidComObjectException

时间:2013-12-12 06:10:30

标签: c# sql database oledb

首先,我想告诉你我的情景:

  1. 我有一个表ID字段为主键,数据类型为整数
  2. 我使用字符串来捕获其他表单上的可用表(我构建     它与其他形式的实例一起)
  3. 我有一个命令按钮,用于删除选中ID的字段     listBox或comboBox
  4. 这样的删除查询命令

    query = "DELETE FROM "+GetTable+" WHERE ID= @id"; cmd.Parameters.AddWithValue("@id",int.Parse(listBoxID.SelectedItem.ToString()));

  5. 其中query as string和GetTable As string,其中从其他表单获取值。

    更新

    这是我的实际代码,我有form1,form2,form3,

    在form1中,我有命令按钮并连接form2

    中的form3
    private static Form2 InstanceForm2;
    public static Form2 NewForm2
    {
       get
       {
          if (InstanceForm2 == null || InstanceForm2.IsDisposed) InstanceForm2 = new Form2();
          return InstanceForm2;
       }
    }
    
    private void commandButtonClickIntoForm2(object sender, Eventargs e)
    {
       NewForm2.Show();
    }
    
    private void commandButtonClickIntoForm3(object sender, Eventargs e)
    {
       NewForm2.NewForm3.Show();
    }
    

    在Form2中,我有一个ListBox,它获取数据库表并将其保存到字符串中并将字符串发送到Form3

    private static Form3 InstanceForm3;
    public static Form3 NewForm3
    {
       get
       {
       if (InstanceForm3 == null || InstanceForm3.IsDisposed) InstanceForm3 = new NewForm3();
       return InstanceForm3;
       }
    }
    
    NewForm3.GetTheTable = listBox1.selecteditem.ToString();
    private void CreateTableButton (object sender,EventArgs e)
    {
        string CreateQuery = "CREATE TABLE " + createTableTextBox.Text +
                             " (ID int IDENTITY(1,1) PRIMARY KEY, " +
                             "NAME NVARCHAR(10), STATUSE NVARCHAR(10))";
        OleDbCommand Create = new OleDbCommand(CreateQuery,con)
        try
        {
            con.Open();
            Create.ExecuteNonQuery();
        }
        catch
        {
            MessageBox.Show("Table Existed","Error");
        }
        finally
        {
            con.Close();
        }
    }
    

    当然使用 using.System.OleDb; 这项工作我在这里的代码中没有提到它,因为它太长而且代码运行成功。

    在Form3中,我有一个 loadDBList函数刷新按钮(再次调用loadDbList)和删除按钮

    public string GetTheTable;
    private void loadDbList()
    {
       string loadquery = "SELECT * FROM "+ GetTheTable;
       con.Open();
       cmd.Connection = con;
       cmd.CommandText = loadquery;
    
       dr = cmd.ExecuteReader();
    
       while(dr.Read())
       {
          listBox1.Items.Add(dr[0].ToString());
          listBox2.Items.Add(dr[1].ToString());
          listBox3.Items.Add(dr[2].ToString());
       }
       con.Close();
    }
    
    private void DeleteFieldButton(object sender,EventArgs e)
    {
       string DeleteQuery = "DELETE FROM "+GetTheTable+" WHERE ID= @id";
       cmd.Parameters.AddWithValue("@id", int.Parse(listBox1.SelectedItem.ToString());
    
       con.Open();
       try
       {
          cmd.Connection = con;
          cmd.CommandText = DeleteQuery;
          cmd.ExecuteNonQuery();
       }
       catch (Exception ex)
       {
          MessageBox.Show("Error:"+ex.ToString(),"Error");
       }
       con.Close();
       loadDbList();
    }
    

    我点击删除按钮后第二次获得的错误(第一次成功了)

    system.Runtime.interopServices.InvalidComObjectException
    

    之后,调用loadDbList()函数的Refresh按钮具有相同的异常,并且在错误发生后奇怪的事情再次点击,再次出错。

    奇怪的是:

    • 在DELETE之前,其他SQL命令没有任何问题 命令已运行。

    我觉得有些东西被处理掉了,但它只是直觉。

    目标:我想从数据库中删除该字段,没有任何例外。

    我该怎么做才能克服这个问题? 我会做任何帮助并接受它。

0 个答案:

没有答案