locked
Connecting to ORACLE 11g Express RRS feed

  • Question

  • User1523848810 posted

    Hi team, i'm new in oracle database and i need a little help...

    i'm using the obsolete class System.Data.OracleClient to connect to the server, but when i connect to it, i dont know how to specify the database to connect, i had put in the connectionstring diferente words like "database", "initial catalog", etc.

    help me...

    this is the code i'm using 

     OracleConnection OraCon = new OracleConnection(appSetting.GetValue("ConnectionString", type: typeof(string)).ToString());
                OraCon.Open();
    
                foreach (DataRow dr in dtResult.Rows)
                {
                    String QueryString = "INSERT INTO TEMPORAL (";
                    foreach (DataColumn dc in dtResult.Columns)
                    {
                        QueryString += dc.ColumnName + ",";
                    }
                    QueryString = QueryString.Substring(0, QueryString.Length -1) + ") Values (";
    
                    foreach (DataColumn dc in dtResult.Columns)
                    {
                        QueryString += dr.ToString() + ",";
                    }
                    QueryString += ")";
    
                    try
                    {
                        OracleCommand OraComm = new OracleCommand(QueryString, OraCon);
                        OraComm.ExecuteNonQuery();
                    }
                    catch (OracleException ex)
                    {
                    }
                }

    the connectiongstring i have is this
    Data Source=LAP;User Id=test;Password=tiger;

    Tuesday, October 30, 2012 11:46 AM

Answers

  • User269602965 posted

    QueryString += dr.ToString() + ",";

    The query will fail.

    1. You are putting a comma at the end of your VALUES string

    2. You should be passing PARAMETERS to BIND variables to INSERT data.

    Imports System.Xml.Linq.XElement
    
      Public Shared Sub updateUnitsActiveFlag(ByVal decQuantity As Decimal)
        ' Insert Quantity into new row Units table'
        Dim OraConnStr As String = ConfigurationManager.ConnectionStrings("OraConnStr").ConnectionString
        Try
          Dim SQL =
          <SQL>
          INSERT INTO {YOURSCHEMANAME}.UNITS
            (UNITS_SEQ, QUANTITY, DATE_CLOSED, DATE_TODAY) 
          VALUES 
            (UNIT_SEQ.NextVal, :BindVarQuantity, :DateClosed, SYSDATE)
          </SQL>
          Using conn As New OracleConnection(OraConnStr)
            Using cmd As New OracleCommand(SQL.Value, conn)
              cmd.Parameters.Clear()
              cmd.Parameters.Add("BindVarQuantity", OracleDbType.Decimal, decQuantity, ParameterDirection.Input)
              cmd.Parameters.Add("DateClosed", OracleDbType.Date, dateDateClosed, ParameterDirection.Input)
              conn.Open()
              cmd.ExecuteNonQuery()
            End Using
          End Using
        Catch ex As Exception
          AppCalls.WriteToEventLog(ex, "Inserting UNITS.QUANTITY failed", "AppCalls.updateUnitsActiveFlag.vb")
        End Try
    End Sub


    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, October 30, 2012 2:46 PM