尝试从visual c#program更新oracle时出现“无效操作。连接已关闭”错误

时间:2014-07-09 19:22:19

标签: c# oracle

我有一个可视化的c#程序正在运行,但每当我尝试更新时,我都会收到连接错误。这是我的代码的样子:

private void Update()
{
    try
    {
        String OneMachineScheduleOrder = "";
        String series = "";
        String oven = "";
        String battery = "";
        int x,y;

        var sortedTextboxes = panel1.Controls
                .OfType<TextBox>() // get all textboxes controls
                .OrderBy(ctrl => ctrl.TabIndex); // order by TabIndex
        foreach (TextBox txt in sortedTextboxes)
        {
            //Console.WriteLine(Convert.ToInt32(txt.TabIndex/2+1) + ": " + txt.Text);
            OneMachineScheduleOrder = (txt.TabIndex / 2 + 1).ToString();
            series = txt.Text.Substring(0, 1);
            oven = txt.Text.Substring(1, 2);
            battery = txt.Text.Substring(4).Trim();
            if (Char.IsLetter(series[0]) && int.TryParse(oven, out y) && int.TryParse(battery, out x) && txt.Text[3].Equals('/'))
            {
                using (OracleConnection con = new OracleConnection(connectString))
                {
                    OracleCommand cmd = connection.CreateCommand();
                    cmd.CommandType = CommandType.Text;
                    cmd.CommandText = "update Oven_Master set SERIES = '" + series + "', OVEN = '" + oven + "', BATTERY = '" + battery + "' where ONE_MACHINE_SCHEDULE_ORDER = '" + OneMachineScheduleOrder + "'";
                    cmd.Connection = con;
                    cmd.ExecuteNonQuery();
                    Console.WriteLine(cmd.CommandText);
                    con.Close();
                }
            }
            else { MessageBox.Show("Number: " + OneMachineScheduleOrder + " Is Invalid!"); }                           
        }
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.ToString());
    }
    finally
    {
        connection.Close();
    }
}

基本上,我在表单上有一堆文本框,以A01/01格式填写。我正在将文本框排序为变量,然后对于每个文本框,我解析出相关数据(OneMachineScheduleOrder,系列,烤箱和电池)。如果数据格式正确,我使用带有全局连接字符串的oracleConnection(并且我使用调试器检查它是否具有正确的值)来创建执行和OracleCommand。否则,提醒用户数据格式错误。

但是,我收到连接打开的错误。我尝试在该行上设置一个断点,我得到con = OracleConnection,所以我可以看到连接。不知道从哪里开始。

1 个答案:

答案 0 :(得分:9)

尝试在执行命令之前调用connection.Open

(是的,我知道对了)