Creating a function

Discussion in 'ASP .Net' started by David, Apr 5, 2007.

  1. David

    David Guest

    Hi,

    This is causing me a few problems as I am not sure how to handle this...

    I have a data access layer. My layer is built in such a way that I can use
    any database type I choose, such as SQL Server, MySQL etc.

    Now, in my layer, I have all my SQL statements and use parameterised
    queries. An example of a parameter is...

    cmd = DL.CreateCommand(sql, conn);

    param = DL.CreateDataParameter();
    param.ParameterName = "@DomainName";
    param.DbType = DbType.String;
    param.Value = DomainName;

    cmd.Parameters.Add(param);

    (DL is the data layer)

    What I would like to do somehow is to call a function and pass the values,
    but I am not sure how the function should be constructed.

    I want to be able to call it something like...

    param = BuildParam("@DomainName", DbType.String, ValueString).

    The reason being is my Datalayer is getting quite big now, much of the space
    is taken by declaring my parameters like what is shown.

    Any help would be appreciated.



    BTW. Thanks Alexei, the keys in my web.config to allow authentication across
    applications worked great.

    --
    Best regards,
    Dave Colliver.
    http://www.AshfieldFOCUS.com
    ~~
    http://www.FOCUSPortals.com - Local franchises available
    David, Apr 5, 2007
    #1
    1. Advertising

  2. Something like this is how I might do it (this is untested "Pseudocode"):

    public IDbCommand AddParameter(IDbCommand cmd, string paramName, DbType
    paramType, object paramValue )
    {

    IDataParameter param = new (whatever the database provider is, e.g.
    SqlParameter)
    param.Name = paramName;
    param.DbType = paramType;
    param.Value =paramValue;
    cmd.Parameters.Add(param)
    return cmd;
    }



    http://msdn2.microsoft.com/en-us/library/system.data.idataparameter_members(vs.71).aspx

    Peter
    --
    Site: http://www.eggheadcafe.com
    UnBlog: http://petesbloggerama.blogspot.com
    Short urls & more: http://ittyurl.net




    "David" wrote:

    > Hi,
    >
    > This is causing me a few problems as I am not sure how to handle this...
    >
    > I have a data access layer. My layer is built in such a way that I can use
    > any database type I choose, such as SQL Server, MySQL etc.
    >
    > Now, in my layer, I have all my SQL statements and use parameterised
    > queries. An example of a parameter is...
    >
    > cmd = DL.CreateCommand(sql, conn);
    >
    > param = DL.CreateDataParameter();
    > param.ParameterName = "@DomainName";
    > param.DbType = DbType.String;
    > param.Value = DomainName;
    >
    > cmd.Parameters.Add(param);
    >
    > (DL is the data layer)
    >
    > What I would like to do somehow is to call a function and pass the values,
    > but I am not sure how the function should be constructed.
    >
    > I want to be able to call it something like...
    >
    > param = BuildParam("@DomainName", DbType.String, ValueString).
    >
    > The reason being is my Datalayer is getting quite big now, much of the space
    > is taken by declaring my parameters like what is shown.
    >
    > Any help would be appreciated.
    >
    >
    >
    > BTW. Thanks Alexei, the keys in my web.config to allow authentication across
    > applications worked great.
    >
    > --
    > Best regards,
    > Dave Colliver.
    > http://www.AshfieldFOCUS.com
    > ~~
    > http://www.FOCUSPortals.com - Local franchises available
    >
    >
    >
    =?Utf-8?B?UGV0ZXIgQnJvbWJlcmcgW0MjIE1WUF0=?=, Apr 5, 2007
    #2
    1. Advertising

  3. This style of coding may help streamline:


    Sub Dothis(byVal DomainName As String, byVal Something As Integer)

    ....

    With cmd.Parameters
    .Add(New SqlParameter("@DomainName", DomainName))
    .Add(New SqlParameter("@Something", Something))
    End With

    ....

    End sub
    Jon Paal [MSMD], Apr 5, 2007
    #3
  4. David

    David Guest

    Fantastic. This lead me onto....

    in my calling location
    cmd.Parameters.Add(AddParameter(DL.CreateDataParameter(), cmd,
    "@DomainName", DbType.String, DomainName));

    public IDataParameter AddParameter(IDataParameter param, IDbCommand cmd,
    string paramName, DbType paramType, object paramValue )
    {
    param.ParameterName = paramName;
    param.DbType = paramType;
    param.Value =paramValue;
    return param;
    }

    Looking at this, I can remove the cmd portion as well.

    Once I understood this, I then went further using your code more closely
    (where I need the cmd portion)

    cmd = AddParameter(DL, cmd, "@DomainName", DbType.String, DomainName);

    public IDbCommand AddParameter(DataAccessLayer.ProviderFactory DL,
    IDbCommand cmd, string paramName, DbType paramType, object paramValue )
    {

    IDataParameter param = DL.CreateDataParameter();
    param.ParameterName = paramName;
    param.DbType = paramType;
    param.Value =paramValue;
    cmd.Parameters.Add(param);

    return cmd;
    }


    Thanks.

    I am gonna have a big job now re-writing my code to use this...

    --
    Best regards,
    Dave Colliver.
    http://www.AshfieldFOCUS.com
    ~~
    http://www.FOCUSPortals.com - Local franchises available
    "Peter Bromberg [C# MVP]" <> wrote in
    message news:...
    > Something like this is how I might do it (this is untested "Pseudocode"):
    >
    > public IDbCommand AddParameter(IDbCommand cmd, string paramName, DbType
    > paramType, object paramValue )
    > {
    >
    > IDataParameter param = new (whatever the database provider is, e.g.
    > SqlParameter)
    > param.Name = paramName;
    > param.DbType = paramType;
    > param.Value =paramValue;
    > cmd.Parameters.Add(param)
    > return cmd;
    > }
    >
    >
    >
    > http://msdn2.microsoft.com/en-us/library/system.data.idataparameter_members(vs.71).aspx
    >
    > Peter
    > --
    > Site: http://www.eggheadcafe.com
    > UnBlog: http://petesbloggerama.blogspot.com
    > Short urls & more: http://ittyurl.net
    >
    >
    >
    >
    > "David" wrote:
    >
    >> Hi,
    >>
    >> This is causing me a few problems as I am not sure how to handle this...
    >>
    >> I have a data access layer. My layer is built in such a way that I can
    >> use
    >> any database type I choose, such as SQL Server, MySQL etc.
    >>
    >> Now, in my layer, I have all my SQL statements and use parameterised
    >> queries. An example of a parameter is...
    >>
    >> cmd = DL.CreateCommand(sql, conn);
    >>
    >> param = DL.CreateDataParameter();
    >> param.ParameterName = "@DomainName";
    >> param.DbType = DbType.String;
    >> param.Value = DomainName;
    >>
    >> cmd.Parameters.Add(param);
    >>
    >> (DL is the data layer)
    >>
    >> What I would like to do somehow is to call a function and pass the
    >> values,
    >> but I am not sure how the function should be constructed.
    >>
    >> I want to be able to call it something like...
    >>
    >> param = BuildParam("@DomainName", DbType.String, ValueString).
    >>
    >> The reason being is my Datalayer is getting quite big now, much of the
    >> space
    >> is taken by declaring my parameters like what is shown.
    >>
    >> Any help would be appreciated.
    >>
    >>
    >>
    >> BTW. Thanks Alexei, the keys in my web.config to allow authentication
    >> across
    >> applications worked great.
    >>
    >> --
    >> Best regards,
    >> Dave Colliver.
    >> http://www.AshfieldFOCUS.com
    >> ~~
    >> http://www.FOCUSPortals.com - Local franchises available
    >>
    >>
    >>
    David, Apr 5, 2007
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Ravi Ambros Wallau
    Replies:
    0
    Views:
    4,070
    Ravi Ambros Wallau
    Jun 1, 2005
  2. James Vanns
    Replies:
    7
    Views:
    7,003
    Evan Carew
    Jan 21, 2004
  3. komal
    Replies:
    6
    Views:
    1,399
    msalters
    Jan 25, 2005
  4. Replies:
    2
    Views:
    883
    Bengt Richter
    Aug 1, 2005
  5. Ravi Ambros Wallau
    Replies:
    0
    Views:
    294
    Ravi Ambros Wallau
    Jun 1, 2005
Loading...

Share This Page