ConnectionString异常在Oracle数据库中

时间:2016-06-05 14:36:14

标签: c# database oracle

我想通过C#应用程序将一些数据插入Oracle数据库。

我一直得到一个如下所示的异常:“ConnectionString尚未正确初始化”。 插入的代码如下:

试             {

                conn.openConnection();

                OracleCommand cmd = new OracleCommand();
                cmd.Connection = conn.Connection;
                cmd.CommandType = CommandType.Text;

               String sqlCommand = "INSERT INTO ComandaDVD (Id_Comanda,Id_Format,Data_Comanda,Id_TipPlata,Pret) VALUES (" +
                    "'" + txt_idComanda.Text + "', " +
                    "'" + txtFormat.Text + "', " +
                    "to_date('" + txtData.Text + "', 'DD-MM-YYYY'), " +
                    "'" + txtIdTipPlata.Text + "', " +
                    "'" + txtPret.Text + "')";

                cmd.CommandText = sqlCommand;

                int result = cmd.ExecuteNonQuery();
                if (result > 0)
                {
                    MessageBox.Show("Comanda cu id_comanda[" + txt_idComanda.Text + "]a fost primita!");

                }
                else
                {
                    MessageBox.Show("Eroare");
                }

                conn.closeConnection();            
        }

        catch (Exception ex)
        {
            MessageBox.Show("Exceptie" + ex.Message);
        }         
    }

我还制作了一个个性化的课程来简化连接处理:  Conexiune_DB类     {

    private OracleConnection conn;
    private static string CONNECTION_STRING = "Data Source=80.96.123.131/ora09;User Id=hr;Password=oracletest;";

    public Conexiune_DB() { conn = new OracleConnection(CONNECTION_STRING); }

    public void openConnection() { conn.Open(); } 

    public void closeConnection() { conn.Dispose(); } 

    public OracleConnection Connection
    {
        get { return conn; }     
    }

}

该异常似乎是因为该类中的'conn.Open'。这很奇怪,因为我之前做了一些插入,我没有任何问题。

提前致谢。

1 个答案:

答案 0 :(得分:0)

我确定会发生这种情况,因为您甚至在为其设置了ConnectionString之前就已经使用conn.openConnection();打开了连接。因此,您应首先将ConnectionString设置为conn,然后再打开它。我不确定您此刻想要使用哪些连接字符串,但如果它们相同,那么只需将conn.Conexiune_DB();放在conn.openConnection();

之上
相关问题