SQL Parameters: Add vs. AddWithValue

I'm in the process of designing an application (Visual C# 2012) that will be working with a SQL database, and have been using parameters in my queries.
My question is if there is any advantages/disadvantages to using AddWithValue over Adding the paramater and then specifying the value and type in a separate instruction (in the C# code)?
I've read a few places that sometimes with AddWithValue, either the .Net framework or SQL doesn't always select the appropriate datatype for the parameter and can cause errors.  But on the other hand, using AddWithValue can reduce the number of lines
of code you have to write (some of my queries might have 10+ parameters, which is small compared to a database for a multi-site Enterprise database system) and thus make your code faster (hopefully).
Opinions as to whether to use Add + extra statements, or just use AddWithValue?

that sometimes with AddWithValue, either the .Net framework or SQL doesn't always select the appropriate datatype for the parameter and can cause errors. 
It's the other way round, see
SqlParameterCollection.AddWithValue
Method => Remarks:
"AddWithValuereplaces the
SqlParameterCollection.Add method
that takes a String and an
Object. The overload of
Add that takes a string and an object was deprecated because of possible ambiguity with the
SqlParameterCollection.Add overload that takes a
String and a
SqlDbType enumeration value where passing an integer with the string could be interpreted as being either the parameter value or the corresponding
SqlDbType value. Use
AddWithValue whenever you want to add a parameter by specifying its name and value."
Olaf Helper
[ Blog] [ Xing] [ MVP]

Similar Messages

  • SQL-Parameters in BC4J?

    Hi
    For performance and security reasons it is usually better to use SQL-Parameters (like :1) in SQL-Queries instead of building the SQL-String directly. I'd like to adopt this best practice but stumbled over a problem: BC4J adds Parameters when applying View-Links. Unfortunately I've no control over the Parameter added. See the following example:
    Assume you have a View-Object with the following Query:
    select NAME from USERS where NAME like :1
    :1 is my custom parameter which I'd like to set using method 'setWhereClauseParams'. This view works fine until this view is used as a detail of a view link. In this case BC4J adds his own SQL-Parameter so that the query looks like that:
    select NAME from USERS where NAME like :1 and (users.USR_ID = :1)
    where the first :1 is my own Parameter and the second :1 is added by BC4J as part of the view link. This certainly does not work.
    Can anyone give me a hint how to use SQL-Parameters correctly in BC4J-Views? Is there a "How To"-Document or something like that which explain how to use SQL-Parameters in BC4J?
    Any hints are appreciated
    Thanks
    Frank Brandstetter

    This way framework automatically adds the new where clause to the existing one, thus eliminating your issueDoes it? I'm not sure! I just tried it out the following example:
    Master ViewObject: GroupsView
    Detail ViewObject: UsersView (ViewLink to GroupsView)
      GroupsViewImpl GrpVo = getGroupsView();
      GrpVo.first();
      UsersViewImpl UsrVo = getUsersView();
      UsrVo.setWhereClause("NAME like :1");
      System.out.println("Query=" + UsrVo.getQuery());This code prints:Query=SELECT Users.USR_ID,Users.GRP_ID,Users.NAME FROM USERS Users WHERE (NAME like :1) AND Users.GRP_ID = :1This does not look as if my issue is solved... :(

  • Unable to install SQL SSRS add-in for SharePoint

    I am unable to install MS SQL SSRS add-in for SharePoint 2010 in a two tier architecture. The following message is displayed while running the Add-in installation:
    "Please ensure Microsoft SharePoint Technologies is installed"
    The environment setup is as mentioned below:
    SharePoint Server
    Has SharePoint 2010 installed
    Hosts Central Administration.
    Has SQL 2008 Reporting Services installed.
    SQL Server:
    Has SQL 2008 R2 installed in SharePoint Integrated Mode
    Has SQL Reporting Services
    Has SharePoint Server installed, configured and joined to two tier farm.
    Could you please help resolve this issue?

    Hi Abhijit,
    According to your description, my understanding is that you got an error when you ran SQL Reporting service add-in for SharePoint 2010.
    Which version add-in did you use? Please you installed the right version.
    There are some reasons for this error:
    You are installing the wrong version of add-in. For example, installing 32bit add-in on x64 machine or vice versa.
    You haven’t install SharePoint on the report server machine
    Your SharePoint installation is not recognized by the add-in. Either cases, you need to reinstall SharePoint.
    Some similar articles for your reference:
    http://geekswithblogs.net/naijacoder/archive/2010/06/28/140653.aspx
    http://blogs.devhorizon.com/reza/2008/10/16/shifting-from-ssrs-2008-native-to-integrated-for-good/
    About installing reporting service add-in, please refer to the links:
    http://stevemannspath.blogspot.com/2012/10/reporting-services-2012-integrated-mode.html
    http://msdn.microsoft.com/en-us/library/aa905871.aspx
    Best Regards
    Wendy
    Forum Support
    Please remember to mark the replies as answers if they help and unmark them if they provide no help. If you have feedback for TechNet Subscriber Support, contact
    [email protected]
    Wendy Li
    TechNet Community Support

  • Sql statement add an "N" literal wher reporting trough oracle 10g and lib eclipse

    <p>the same select created with crystal report 9 and oracle 8.04 with crystal report XI and oracle 10G have some difference. In the crystal report XI the statement contains an &#39;N&#39; literal that should not be there.</p><p>I have used the Service pack 1 of Business Object XI r2 on the server and here i haven&#39;t problem.</p><p>When I use the same report throught an applet with the CRXI library&#39;s I have the problem of "N" literal</p><p>I work with Eclipse, java, CRXI, and oracle10g</p>

    JRC/Crystal Report XI generated SQL automatically add an "N" literal in front of some values which can not even execute in the SQL*Plus.

  • SQL *NET Add-On

    I want to install the Personnel Oracle 8 (Student Edition) with
    SQL *NET Add-On Version 2.2.2.1.1 on Windows 95(win98), but the
    system tell me that i have to install the SQL *NET Add-On
    version 2.2.2.0.0 (patch) first.
    Anybody can help me ?
    Thanks
    Best Regards
    Carlos Barbosa
    null

    Carlos,
    This discussion forum is specifically targeted to answer
    questions on the Oracle Migration Workbench and Migration
    issues. I am unfamiliar with this product and unfortunately
    do not have any information that may help. Perhaps if you
    called Oracle support or check out the database section
    on OTN. There may be information there that will help.
    Regards,
    Marie
    Carlos Barbosa (guest) wrote:
    : I want to install the Personnel Oracle 8 (Student Edition) with
    : SQL *NET Add-On Version 2.2.2.1.1 on Windows 95(win98), but the
    : system tell me that i have to install the SQL *NET Add-On
    : version 2.2.2.0.0 (patch) first.
    : Anybody can help me ?
    : Thanks
    : Best Regards
    : Carlos Barbosa
    Oracle Technology Network
    http://technet.oracle.com
    null

  • Logging SQL parameters in Database Adapter??

    Hi there!!
    We'd like to be able to log all the parameters that are used in a SQL operation in Database Adapters. So far, we've been able to log the query with '?' instead of the real parameters. For example: select t.* from tsupcontact t WHERE t.idsupplier = ? AND t.idproject IS NULL AND (t.iduser = ? OR ? IS NULL)
    But we need to be able to see the actual parameters that are passed to the query (we're having some trouble with the database adapters and we need to see what's really going on the database).
    Any ideas? Thanks in advance!!

    You need to edit the the aslbdebug.xml file in ./user_projects/domains/<your domain>. Then go into the weblogic console and change the levels to debug. You will get lots of debugging. Below are the settings in my file.
    <java:sb-debug-logger xmlns:java="java:com.bea.wli.debug">
    <java:alsb-stages-transform-runtime-debug>true</java:alsb-stages-transform-runtime-debug>
    <java:alsb-alert-manager-debug>false</java:alsb-alert-manager-debug>
    <java:alsb-credential-debug>false</java:alsb-credential-debug>
    <java:alsb-jms-reporting-provider-debug>false</java:alsb-jms-reporting-provider-debug>
    <java:alsb-management-credential-debug>false</java:alsb-management-credential-debug>
    <java:alsb-management-dashboard-debug>false</java:alsb-management-dashboard-debug>
    <java:alsb-management-debug>false</java:alsb-management-debug>
    <java:alsb-management-user-mgt-debug>false</java:alsb-management-user-mgt-debug>
    <java:alsb-module-debug>true</java:alsb-module-debug>
    <java:alsb-monitoring-aggregator-debug>false</java:alsb-monitoring-aggregator-debug>
    <java:alsb-monitoring-debug>false</java:alsb-monitoring-debug>
    <java:alsb-pipeline-debug>true</java:alsb-pipeline-debug>
    <java:alsb-security-wss-debug>false</java:alsb-security-wss-debug>
    <java:alsb-service-account-manager-debug>false</java:alsb-service-account-manager-debug>
    <java:alsb-service-provider-manager-debug>false</java:alsb-service-provider-manager-debug>
    <java:alsb-service-repository-debug>false</java:alsb-service-repository-debug>
    <java:alsb-service-security-manager-debug>false</java:alsb-service-security-manager-debug>
    <java:alsb-service-validation-debug>false</java:alsb-service-validation-debug>
    <java:alsb-test-console-debug>true</java:alsb-test-console-debug>
    <java:alsb-transports-debug>true</java:alsb-transports-debug>
    <java:alsb-uddi-debug>true</java:alsb-uddi-debug>
    <java:alsb-wsdl-repository-debug>false</java:alsb-wsdl-repository-debug>
    <java:alsb-wspolicy-repository-debug>false</java:alsb-wspolicy-repository-debug>
    <java:alsb-security-encryption-debug>false</java:alsb-security-encryption-debug>
    <java:alsb-security-module-debug>false</java:alsb-security-module-debug>
    <java:alsb-sources-debug>false</java:alsb-sources-debug>
    <java:alsb-custom-resource-debug>true</java:alsb-custom-resource-debug>
    <java:alsb-mqconnection-debug>false</java:alsb-mqconnection-debug>
    <java:alsb-throttling-debug>false</java:alsb-throttling-debug>
    <java:alsb-flow-resource-debug>false</java:alsb-flow-resource-debug>
    <java:alsb-flow-transport-debug>false</java:alsb-flow-transport-debug>
    <java:alsb-flow-deployment-debug>false</java:alsb-flow-deployment-debug>
    <java:alsb-debugger-debug>true</java:alsb-debugger-debug>
    </java:sb-debug-logger>

  • Passing SQL-Parameters to Oracle-Reports from java

    hello,
    i want to write an application in the following way:
    on a java-frontend an user can select values for different
    parameters. these values should be to transferred to the sql-
    query of the reports rdf file. what is the syntax of an
    parameter passed from java to oracle reports.
    does this work with runtime.exec()... ?
    does anybody have an example.
    any help would be very appreciated.
    greetings
    Thorsten Lorenz

    In order to pass the parameters to report rdf, you can create user parameters in the oracle report builder, hook up the parameters with query. For example, you can create user parameter P_DEPTNO, then create a sql query: select * from dept where deptno = :P_DEPTNO. When you run this report, you give P_DEPTNO=10 as parameter, the rdf will generate report that only prints out department 10's information.
    Once you have this kind of report created, you have several ways to achieve what you want.
    1. use rwrun60. In your java program, you can invoke rwrun60 via runtime.exec(cmd) where cmd = rwrun60 report=dept.rdf userid=scott/tiger@orcl destype=file desformat=pdf desname=dept.pdf p_deptno=<value_from_java_app>
    2. similarly, use rwcli60. the cmd would be:
    cmd = rwcli60 server=repserver report=dept.rdf userid=scott/tiger@orcl destype=file desformat=pdf desname=dept.pdf p_deptno=<value_from_java_app>
    3. use rwcgi60. Instead of using runtime.exec, you should use java URL object to run the report in the web environment.
    option 2 & 3 takes advantage of powerful functionality of reports server, and it is much more scale than option 1.

  • SQL Loader add fixed string

    hell o oracle community,
    I am trying to load some recods into the database with sql loader. It works fine so far. But now I want to add an extra column with a fixed text, that is not in the csv file, but a field I need to be filled in the table. This is the script I have so far:
    OPTIONS (BINDSIZE = 100000, SKIP = 1)
    LOAD DATA
    CHARACTERSET WE8ISO8859P1
    INFILE 'D:\Import\dtv_backup\partner.csv' BADFILE 'C:\SQLLoader\BadPartner.txt' DISCARDFILE 'C:\SQLLoader\DiscardPartner.txt'
    APPEND
    INTO TABLE PartnerBM
    FIELDS TERMINATED BY ';' OPTIONALLY ENCLOSED BY '"'
    TRAILING NULLCOLS
    (CK_Quellsystem here_I _need_the_fixed_text
    CK_QuelleID CHAR,
    ID_NR CHAR,
    CallcenterName CHAR,
    Wohnort CHAR,
    Strasse CHAR,
    PLZ CHAR,
    Telefon1 CHAR,
    Telefon2 CHAR,
    Fax1 CHAR,
    Fax2 CHAR,
    Beschreibung CHAR,
    Anrede1 CHAR,
    Ansprechpartner1 CHAR,
    Anrede2 CHAR,
    Ansprechpartner2 CHAR,
    Kontaktdaten1 CHAR,
    Kontaktdaten2 CHAR,
    Mail CHAR
    I already tried to use "Text" bit it did not work, for example when I do it with SYSDATE it works. Any suggestions ho to add the fixed Text ?
    Ikrischer

    thought I found a solution, but the way I used the script is not inserting the content from the first column
    Finally found it, CONSTANT is the keyword I need...
    Edited by: user11979777 on 25.06.2010 05:46

  • Vote/Comment on SQL Developer Add-in: CopyAsHtml

    Hi,
    I'm not sure if this is against any rules, but I wanted to make the SQL Developer community aware of this feature request and would ask you to simply add a vote/comment on this if you used this feature from SQL Developer 3 to allow you to copy code out of SQL Developer with full syntax highlighting, font, and indentations into 3rd party programs.
    Vote here:
    https://apex.oracle.com/pls/apex/f?p=43135:7:4683410243712::NO::P7_ID:41621
    Thanks.

    Ok its working again... Initial build of SQL Developer 1.5 (5338) was not showing the geom layers on the georaptor....
    I am happy to report that with SQL Developer 1.5.1 (5440) Georaptor layers on the Spatial view are working again.
    Thanks to the anonymous developers that fixed it!

  • Log the SQL parameters used by ORM

    Hi Everyone,
    I have read Rupesh's blog post;
    http://www.rupeshk.org/blog/index.php/2009/07/coldfusion-orm-how-to-log-sql/
    and have appropriately changed the log4j setting
    ### Also log the parameter binding to the prepared statements.
    log4j.logger.org.hibernate.type=DEBUG
    But I still do not see the values of query parameters in my log files.
    All I get is the auestion marks;
    09/15 12:35:57 [ajp-bio-8014-exec-3] HIBERNATE DEBUG -
        insert
        into
            audit_log
            (description, created, userID)
        values
    Does anyone have any ideas? And as always - thanks in advance!
    Gavin.
    Message was edited by: GavinBaumanis

    Easy but you need to extract it from Microsoft Generic Report Pack. 
    So.. the procedure is as follows:
    1) You export and unseal Management Pack from your SCOM using
    Boris's OpsgMgr tools (MPViewer
    2.3.3)
    2) Ok you've got unsealed xml, now the tricky part, use
    MpElementsExtract tool, example of usage:
    MPElementsExtract.exe <MP.xml> /ex /destination:<destination>
    That you way you get several folders out of mp:
    DWScripts, DWSubScripts, LinkedReports, ReportResources, Reports
    Take a look into content of first 2, there will be pure sql scripts, and rdl's are in Reports folder :)
    Other way is to just use SQL profiler and catch SQL query while generating report. 
    --- Jeff (Netwrix)

  • Regapp.sql parameters

    I am trying to register APEX as a partner application using regapp.sql command. Where on the AS would I find the values for the following parameters?
    p_site_token
    p_site_id
    p_login_url
    p_encryption_key
    Thanks

    sch5479,
    Have a look at the following link -
    http://www.oracle.com/technology/products/database/application_express/howtos/sso_partner_app.html
    It describes exactly how to setup an Apex application as a partner Application with Oracle SSO

  • PL/SQL Parameterized Exceptions?

    I develop on the Oracle EBS platform and so I have to develop using multiple general purpose programming languages.  Mainly PL/SQL and Java but we also use Ruby and Korn Shell for example.  One thing I have trouble with in PL/SQL is exception handling.  I would really like to be able to provide additional information in an Exception in PL/SQL but don't know how to do so.  One way would be to define an exception with parameters.  Thus providing some slots to stick information into when raising an exception and then using the information when the exception is caught.  I've used raise_application_error that provides slots for a number and text but that's not really my idea of a good solution to this issue.  I like the Ruby Exception object approach myself that allows you to define the slots yourself via sub-classing an Exception class.  I've used OO PL/SQL successfully in certain solutions and I know it works but there are some serious shortcomings that make OO PL/SQL a bit of a no-mans land in my opinion so I think an Exception class in PL/SQL is probably not the way to go at this point but I could be wrong.  So I thought how about defining exceptions with parameters much like a procedure or function.  Is this coming or are there ways to provide additional information in an exception that you have used successfully?
    Thank you

    I got an error when I compiled your procedure
    SQL> create or replace procedure myexception (v  number) is
      2    v1   emp.empno%type;
      3    begin
      4    select empno into v1 from emp where empno=v;
      5    exception when no_data_found then
      6    dbms_output.put_line('No data found');
      7    Do first thing----
      8    when too_many_rows then
      9    dbms_output.put_line('Too many rows');
    10    Do second thing----
    11    when others then
    12    --Do third thing
    13    end;
    14    .
    15   /
    Warning: Procedure created with compilation errors.
    SQL> sho err
    Errors for PROCEDURE MYEXCEPTION:
    LINE/COL ERROR
    13/3     PLS-00103: Encountered the symbol "END" when expecting one of the
             following:
             begin case declare exit for goto if loop mod null pragma
             raise return select update while with <an identifier>
             <a double-quoted delimited-identifier> <a bind variable> <<
             close current delete fetch lock insert open rollback
             savepoint set sql execute commit forall merge pipe
    SQL> create or replace procedure myexception (v  number) is
      2    v1   emp.empno%type;
      3  begin
      4    select empno into v1 from emp where empno=v;
      5  exception when no_data_found then
      6    dbms_output.put_line('No data found');
      7    Do first thing----
      8  when too_many_rows then
      9    dbms_output.put_line('Too many rows');
    10    Do second thing----
    11  when others then
    12    --Do third thing
    13 dbms_output.put_line(SQLERRM);
    14  end;
    15  /
    Procedure created.
    SQL>

  • Input Elements & PL/SQL Parameters

    I have a FORM open/close HTML block on top...then below the block I have inputs...***THOSE INPUTS ARE NOT TO BE TARGETED TO PARAMETERS ***
    How can one prevent those input from being intepreted as parameter entries W/O removing the name attribute ?
    Thanks
    <form ACTION="wict.accept" METHOD="POST" name="ct_flow" id="CTFlowForm">
    blah..blah...
    </form>
    <input name="day" type="checkbox" checked="checked" onclick="return false">Daily
    <input name="week" type="checkbox" onclick="return false">Weekly

    Dear "50122",
    You may have landed in the wrong forum. If your question is actually about the Oracle HTML DB product, please provide a clear question related to HTML DB and we'll try to help.
    -Scott
    P.S. If you like, you can show your name in this forum instead of a numeric ID by clicking on 'Forum Settings' and setting 'Name visible to others' to Yes.

  • SQL*Plus add-on for Instant Client Package on Solaris-SPARC 32bits ???

    Hi,
    I need sqlplus for a solaris 32bits platform, but following this page
    http://www.oracle.com/technology/tech/oci/instantclient/instantclient.html, if I choose Solaris 32-bit (SPARC), it doesn't give a link to download Instant Client Package - SQL*Plus (as it does if I choose Solaris 64-bit (SPARC) or Windows 32-bit).
    I don't understand why... :(

    SQL*Plus is not supported on the 32-bit versions of 64-bit platforms. Please use the 64-bit version of SQL*Plus. Thanks!

  • Overloaded parameters in PL/SQL packages

    Will this driver support calls to packaged stored procedures with overloaded parameters?

    Here's a quick and dirty sample:
    Database:
    create or replace package overload_test as
      function overloaded (p_in in varchar2) return varchar2;
      function overloaded (p_in in number) return varchar2;
    end;
    create or replace package body overload_test as
      function overloaded (p_in in varchar2) return varchar2 is
      begin
        return 'You called the varchar2 overload with: ' || p_in;
      end;
      function overloaded (p_in number) return varchar2 is
      begin
        return 'You called the number overload with: ' || to_char(p_in);
      end;
    end;
    /C# code:
    using System;
    using System.Data;
    using Oracle.DataAccess.Client;
    using Oracle.DataAccess.Types;
    namespace OverloadTest
      class Program
        static void Main(string[] args)
          // connect to local db using o/s authentication
          OracleConnection con = new OracleConnection("User Id=/; Enlist=false; Pooling=false");
          con.Open();
          // create the command object
          OracleCommand cmd = con.CreateCommand();
          cmd.CommandText = "overload_test.overloaded";
          cmd.CommandType = CommandType.StoredProcedure;
          // create the return value parameter
          OracleParameter p_retval = new OracleParameter(":p_retval", OracleDbType.Varchar2, 80, "", ParameterDirection.ReturnValue);
          cmd.Parameters.Add(p_retval);
          // create the input parameter for the number overload
          OracleParameter p_in = new OracleParameter(":p_in", OracleDbType.Decimal, 42, ParameterDirection.Input);
          cmd.Parameters.Add(p_in);
          // call the number overloaded stored proc
          cmd.ExecuteNonQuery();
          // write out the return value
          Console.WriteLine(p_retval.Value);
          // change the input parameter for the varchar2 overload
          p_in.OracleDbType = OracleDbType.Varchar2;
          p_in.Value = "Forty-Two";
          // reset parameters
          cmd.Parameters.Clear();
          cmd.Parameters.Add(p_retval);
          cmd.Parameters.Add(p_in);
          // call the varchar2 overloaded stored proc
          cmd.ExecuteNonQuery();
          // write out the return value
          Console.WriteLine(p_retval.Value);
          // clean up
          p_in.Dispose();
          p_retval.Dispose();
          cmd.Dispose();
          con.Dispose();
          // prevent console window from closing automatically when running from vs env
          Console.WriteLine("ENTER to continue...");
          Console.ReadLine();
    }Output on my system:
    You called the number overload with: 42
    You called the varchar2 overload with: Forty-Two
    ENTER to continue...Hope that helps a bit,
    Mark

Maybe you are looking for