0
public void comboboxes()
{
    using (OracleConnection conn = new OracleConnection("Data Source=localhost;Persist Security Info=True;User ID=kursinis1;Password=1234;Unicode=True"))
    {
        try
        {
            conn.Open();
            comboBox1.Items.Clear();
            DataSet dsetas1 = new DataSet();
            OracleDataAdapter data1 = new OracleDataAdapter("select aut_id, (aut_vardas || ' ' || aut_pavarde) AS autorius from autoriai", conn);
            data1.SelectCommand.CommandType = CommandType.Text;
            data1.Fill(dsetas1);
            dsetas1.Dispose();
            data1.Dispose();
            conn.Close();

            comboBox1.DataSource = dsetas1.Tables[0];
            comboBox1.DisplayMember = "autorius";
            comboBox1.ValueMember = "aut_id";

        }
        catch (Exception ex)
        {
            MessageBox.Show("Can not open connection ! ");
        }
    }
}

当我第一次调用函数时,它可以工作(将数据填充到复选框),但是当我尝试第二次调用函数时(通过单击按钮)。它显示我的连接未打开的错误:

Error: Items collection cannot be modified when the DataSource is set.

问题是什么?

4

2 回答 2

1

首先,您甚至不需要ComboBox.Items在使用DataBinding.

其次,您要在将结果集绑定到您的控件之前处理您的DataSet和之前的处理。DataAdapterComboBox

第三,using块的使用定义了你的变量应该被使用的范围,以及它们应该被自动释放的范围。

我注释掉了您不需要的行并添加了一个finally子句,该子句在您的try...catch块结束时执行。

public void comboboxes() {
    using (OracleConnection conn = new OracleConnection("Data Source=localhost;Persist Security Info=True;User ID=kursinis1;Password=1234;Unicode=True")) {
        try {
            conn.Open();
            //comboBox1.Items.Clear();
            DataSet dsetas1 = new DataSet();
            OracleDataAdapter data1 = new OracleDataAdapter("select aut_id, (aut_vardas || ' ' || aut_pavarde) AS autorius from autoriai", conn);
            data1.SelectCommand.CommandType = CommandType.Text;
            data1.Fill(dsetas1);
            //dsetas1.Dispose();
            //data1.Dispose();
            //conn.Close();

            comboBox1.DataSource = dsetas1.Tables[0];
            comboBox1.DisplayMember = "autorius";
            comboBox1.ValueMember = "aut_id";
        } catch (Exception ex) { MessageBox.Show("Can not open connection ! "); }
        finally {
            if (ConnectionState.Open == conn.State) conn.Close();
        }
    }
}

如果您希望确保您的DataSetDataAdapter在不再需要它们时立即得到处置,请将它们放在using块中。

using (var cnx = new OracleConnection(connectionString)) 
    using (var ds = new DataSet())
        using (var da = new OracleDataAdapter(query)) {
            da.SelectCommand.CommandType = CommandType.Text;

            if (ConnectionState.Closed == cnx.State) cnx.Open();

            da.Fill(ds);

            comboBox1.DataSource = ds.Tables[0];
            comboBox1.DisplayMember = "autorius";
            comboBox1.ValueMember = "aut_id";                                 
        }

using所有这三个:当您的代码退出块时,您的连接、数据集和数据适配器将自动处理。

或者,如果您愿意,您也可以将连接的开口包围在一个try...catch...finally块中,并确保无论发生什么都关闭您的连接。

此外,最好让异常在应用程序中冒泡,以便在它发生时获得完整的堆栈跟踪和适当的异常,以便获得解决问题所需的所有详细信息。

try...catch当它们被优雅地处理时,你会出现异常。例如,以日志记录为例,然后可能只是简单地将 GUI 重新抛出给您,以便您可以通过适当的错误消息等来优雅地处理它。

于 2014-04-19T19:56:30.027 回答
1

在使用它绑定到组合框之前,您正在处理 DataSet。数据集的生命周期必须等于组合框。

您需要使 DataSet 成为拥有组合框的类的成员(并在释放该类时释放),或者在释放 DataSet 之前将表中的数据复制到另一个作为类成员的表或对象列表中,并且而是绑定到该对象。

public partial class MainForm : Form
{
    private DataTable dataTable = new DataTable();

    public MainForm()
    {
        InitializeComponent();

        comboBox1.DataSource = dataTable;
        comboBox1.DisplayMember = "autorius";
        comboBox1.ValueMember = "aut_id";
    }

    public void comboboxes()
    {
        using (OracleConnection conn = new OracleConnection("Data Source=localhost;Persist Security Info=True;User ID=kursinis1;Password=1234;Unicode=True"))
        {
            conn.Open();
            using (OracleDataAdapter data1 = new OracleDataAdapter("select aut_id, (aut_vardas || ' ' || aut_pavarde) AS autorius from autoriai", conn))
            {
                data1.SelectCommand.CommandType = CommandType.Text;
                data1.Fill(dataTable);
            }
        }
    }
}

(未经测试,但类似这样)

于 2014-04-19T19:45:12.690 回答