尝试从 Visual C# 程序更新 oracle 时出现“无效操作。连接已关闭”错误

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24662125/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-19 02:25:09  来源:igfitidea点击:

Getting "Invalid Operation. The Connection is closed" error while trying to update oracle from visual c# program

c#oracle

提问by Alex Kibler

I've got a visual c# program running, but I'm getting a connection is closed error whenever I try to update. Here's what my code looks like:

我有一个可视化 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();
    }
}

Basically, I've got a bunch of textboxes on the form that are filled in in the format A01/01. I'm sorting the textboxes into a variable, and then for each textbox, I parse out the relevent data (OneMachineScheduleOrder, series, oven, and battery). If the data is in the right format, I use an oracleConnection with a connectionstring that is global (and I checked with the debugger that it has the correct value) to create an execute and OracleCommand. Otherwise, alert the user that the data is in the wrong format.

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

However, I'm getting an error that the connection is open. I tried putting a breakpoint on that line, and I'm getting that con = OracleConnection, so I can see that there isa connection. No idea where to go from here.

但是,我收到连接已打开的错误消息。我试图把在该行断点,我理解的con = OracleConnection,这样我就可以看到有一个连接。不知道从这里去哪里。

回答by John Nicholas

try calling connection.Open before executing your command

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

(yes, i know lol right)

(是的,我知道哈哈对)