473,395 Members | 1,442 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Creating a function

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
Apr 5 '07 #1
3 1333
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/lib...rs(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
Apr 5 '07 #2
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
Apr 5 '07 #3
Fantastic. This lead me onto....

in my calling location
cmd.Parameters.Add(AddParameter(DL.CreateDataParam eter(), 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]" <pb*******@yahoo.yabbadabbadoo.comwrote in
message news:70**********************************@microsof t.com...
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/lib...rs(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

Apr 5 '07 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

20
by: svend | last post by:
I'm messing with some code here... Lets say I have this array: a1 = ; And I apply slice(0) on it, to create a copy: a2 = a1.slice(0); But this isn't a true copy. If I go a1 = 42, and then...
0
by: James Fortune | last post by:
Here is an example of Access creating a single page PDF file. The text in the textbox is scaled to fit horizontally into a grey box 100 pixels wide that is fontsize pixels high. Clicking the...
1
by: Dixie | last post by:
I wish to add some fields to an existing table in code. I am using the following code from rkc. CurrentDb.Execute ("ALTER TABLE MyTable ADD MyNewField Text 25") This works , but I need to also set...
8
by: Nanda | last post by:
hi, I am trying to generate parameters for the updatecommand at runtime. this.oleDbDeleteCommand1.CommandText=cmdtext; this.oleDbDeleteCommand1.Connection =this.oleDbConnection1;...
15
by: Carlos Lozano | last post by:
Hi, What is the right way to create an OCX COM component. The component is already registerred, but can't create an instance. I am using the reference to the interop module created. If I use...
6
by: Simon Verona | last post by:
I would normally use code such as : Dim Customer as new Customer Dim t as new threading.thread(AddressOf Customer.DisplayCustomer) Customer.CustomerId=MyCustomerId t.start Which would create...
1
by: CES | last post by:
All, I was wondering if someone could point me to a tutorial on creating & accessing functions from within a wrapper function. I've created a group of functions related to a timer event. All...
17
Motoma
by: Motoma | last post by:
This article is cross posted from my personal blog. You can find the original article, in all its splendor, at http://motomastyle.com/creating-a-mysql-data-abstraction-layer-in-php/. Introduction:...
2
by: ChrisO | last post by:
I've been pretty infatuated with JSON for some time now since "discovering" it a while back. (It's been there all along in JavaScript, but it was just never "noticed" or used by most until...
1
by: kaens | last post by:
So, I have a class that has to retrieve some data from either xml or an sql database. This isn't a problem, but I was thinking "hey, it would be cool if I could just not define the functions for...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.