473,846 Members | 1,914 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

OLEDB Provider for SQL SERVER + Parameterized Queries

Hi,

I currently have an application that connects to an MS ACCESS database. This
application uses an OLEDB connection string for MS ACCESS.
Now, I'd like to upsize the application so I converted the db to SQL SERVER
and this went well.

Next thing I changed the connection string from OLEDB for MS ACCESS to OLEDB
for SQL SERVER.
I expected this to work resonably well, however I found that it never worked
at all.
The reason is that I have done all my MS ACCESS queries using dynamic SQL
and Parameterized queries like so

objCMD.CommandT ext = "SELECT COUNT(*) AS UserCount FROM tblSecurity WHERE
UserName=@UserN ame AND Secretword=@Pas sword"
This seems to work fine for access, But the OLEDB provider for SQL SERVER
complains bitterly about this, for example, attempting to execurte the above
query with attached parameters gives me the following error

Must declare the variable '@UserName'.

Can anybody please explain why these two database will not work with the
same syntax even though I am using an OLEDB connection string / Provider for
both.
I have included my full code below.
Please note that this works with an MS ACCESS OLEDB connection string but
NOT an OLEDB SQL SERVER CONNECTION STRING.

many thanks in advance.

cheers

martin.
=============== =============== =============== =============== =============== =============== ===============

Dim objConn As System.Data.Ole Db.OleDbConnect ion = New
System.Data.Ole Db.OleDbConnect ion()
Dim objCMD As System.Data.Ole Db.OleDbCommand = New
System.Data.Ole Db.OleDbCommand ()
Try
'Trace.Warn("Co n string", OleDbConnection 1.ConnectionStr ing)
objConn.Connect ionString() = Application("Co nnectionString" )
objCMD.Connecti on = objConn
objConn.Open()
Trace.Warn("Con string", objConn.Connect ionString())
objCMD.CommandT ext = "SELECT COUNT(*) AS UserCount FROM
tblSecurity WHERE UserName=@UserN ame AND Secretword=@Pas sword"
Trace.Warn("Dat abase", "database open")
Dim Param1 As System.Data.Ole Db.OleDbParamet er
Dim Param2 As System.Data.Ole Db.OleDbParamet er
Param1 = objCMD.Paramete rs.Add("@UserNa me",
TextBox1.Text.T rim())
Param2 = objCMD.Paramete rs.Add("@Passwo rd",
Password1.Value .Trim())
intNumUsers = objCMD.ExecuteS calar() 'Execute the query
'ERROR THROWN HERE WORKS ON ACCESS BUT NOT SS
Trace.Warn("Dat abase", "No database error occured")
Catch err As Exception
Dim strErrMessage As String
strErrMessage = "Exception thrown function btnLogin_Click" &
Request.ServerV ariables("SCRIP T_NAME") & "<br>"
Trace.Write("ER ROR", err.Message.ToS tring)
'Response.End()
'EmailException (err.Message, strErrMessage)
Finally
If objConn.State = ConnectionState .Open Then
Try
'Attempt to close the connection
objConn.Close()
Catch err As Exception
'Catch any error that may occur
Dim strErrMessage As String
strErrMessage = "Exception thrown attemping to close
after checking the user is in admin. File " &
Request.ServerV ariables("SCRIP T_NAME") & "<br>"
EmailException( err.Message, strErrMessage)
End Try
End If
End Try

Nov 19 '05 #1
2 2868
I think as part of your migration to SQL Server you should migrate your
dynamic sql code to stored procedures, I'm not sure that there is anything
you can do to make the code you have work the way it is. Having that said if
your going to have to change it you might as well use sp's.

"Martin" wrote:
Hi,

I currently have an application that connects to an MS ACCESS database. This
application uses an OLEDB connection string for MS ACCESS.
Now, I'd like to upsize the application so I converted the db to SQL SERVER
and this went well.

Next thing I changed the connection string from OLEDB for MS ACCESS to OLEDB
for SQL SERVER.
I expected this to work resonably well, however I found that it never worked
at all.
The reason is that I have done all my MS ACCESS queries using dynamic SQL
and Parameterized queries like so

objCMD.CommandT ext = "SELECT COUNT(*) AS UserCount FROM tblSecurity WHERE
UserName=@UserN ame AND Secretword=@Pas sword"
This seems to work fine for access, But the OLEDB provider for SQL SERVER
complains bitterly about this, for example, attempting to execurte the above
query with attached parameters gives me the following error

Must declare the variable '@UserName'.

Can anybody please explain why these two database will not work with the
same syntax even though I am using an OLEDB connection string / Provider for
both.
I have included my full code below.
Please note that this works with an MS ACCESS OLEDB connection string but
NOT an OLEDB SQL SERVER CONNECTION STRING.

many thanks in advance.

cheers

martin.
=============== =============== =============== =============== =============== =============== ===============

Dim objConn As System.Data.Ole Db.OleDbConnect ion = New
System.Data.Ole Db.OleDbConnect ion()
Dim objCMD As System.Data.Ole Db.OleDbCommand = New
System.Data.Ole Db.OleDbCommand ()
Try
'Trace.Warn("Co n string", OleDbConnection 1.ConnectionStr ing)
objConn.Connect ionString() = Application("Co nnectionString" )
objCMD.Connecti on = objConn
objConn.Open()
Trace.Warn("Con string", objConn.Connect ionString())
objCMD.CommandT ext = "SELECT COUNT(*) AS UserCount FROM
tblSecurity WHERE UserName=@UserN ame AND Secretword=@Pas sword"
Trace.Warn("Dat abase", "database open")
Dim Param1 As System.Data.Ole Db.OleDbParamet er
Dim Param2 As System.Data.Ole Db.OleDbParamet er
Param1 = objCMD.Paramete rs.Add("@UserNa me",
TextBox1.Text.T rim())
Param2 = objCMD.Paramete rs.Add("@Passwo rd",
Password1.Value .Trim())
intNumUsers = objCMD.ExecuteS calar() 'Execute the query
'ERROR THROWN HERE WORKS ON ACCESS BUT NOT SS
Trace.Warn("Dat abase", "No database error occured")
Catch err As Exception
Dim strErrMessage As String
strErrMessage = "Exception thrown function btnLogin_Click" &
Request.ServerV ariables("SCRIP T_NAME") & "<br>"
Trace.Write("ER ROR", err.Message.ToS tring)
'Response.End()
'EmailException (err.Message, strErrMessage)
Finally
If objConn.State = ConnectionState .Open Then
Try
'Attempt to close the connection
objConn.Close()
Catch err As Exception
'Catch any error that may occur
Dim strErrMessage As String
strErrMessage = "Exception thrown attemping to close
after checking the user is in admin. File " &
Request.ServerV ariables("SCRIP T_NAME") & "<br>"
EmailException( err.Message, strErrMessage)
End Try
End If
End Try

Nov 19 '05 #2
I agree entirely, you are totally right - If you use SQL SERVER the sp's are
definatly the way to go.
However I would be interested to find out why parameterised queries work
with the OLEDB provider for MS ACCESS but NOT with the OLEDB provider for
SQL SERVER.

"Alien2_51" <da************ ***@monacocoach .removeme.com> wrote in message
news:5C******** *************** ***********@mic rosoft.com...
I think as part of your migration to SQL Server you should migrate your
dynamic sql code to stored procedures, I'm not sure that there is anything
you can do to make the code you have work the way it is. Having that said
if
your going to have to change it you might as well use sp's.

"Martin" wrote:
Hi,

I currently have an application that connects to an MS ACCESS database.
This
application uses an OLEDB connection string for MS ACCESS.
Now, I'd like to upsize the application so I converted the db to SQL
SERVER
and this went well.

Next thing I changed the connection string from OLEDB for MS ACCESS to
OLEDB
for SQL SERVER.
I expected this to work resonably well, however I found that it never
worked
at all.
The reason is that I have done all my MS ACCESS queries using dynamic SQL
and Parameterized queries like so

objCMD.CommandT ext = "SELECT COUNT(*) AS UserCount FROM tblSecurity WHERE
UserName=@UserN ame AND Secretword=@Pas sword"
This seems to work fine for access, But the OLEDB provider for SQL SERVER
complains bitterly about this, for example, attempting to execurte the
above
query with attached parameters gives me the following error

Must declare the variable '@UserName'.

Can anybody please explain why these two database will not work with the
same syntax even though I am using an OLEDB connection string / Provider
for
both.
I have included my full code below.
Please note that this works with an MS ACCESS OLEDB connection string but
NOT an OLEDB SQL SERVER CONNECTION STRING.

many thanks in advance.

cheers

martin.
=============== =============== =============== =============== =============== =============== ===============

Dim objConn As System.Data.Ole Db.OleDbConnect ion = New
System.Data.Ole Db.OleDbConnect ion()
Dim objCMD As System.Data.Ole Db.OleDbCommand = New
System.Data.Ole Db.OleDbCommand ()
Try
'Trace.Warn("Co n string",
OleDbConnection 1.ConnectionStr ing)
objConn.Connect ionString() =
Application("Co nnectionString" )
objCMD.Connecti on = objConn
objConn.Open()
Trace.Warn("Con string", objConn.Connect ionString())
objCMD.CommandT ext = "SELECT COUNT(*) AS UserCount FROM
tblSecurity WHERE UserName=@UserN ame AND Secretword=@Pas sword"
Trace.Warn("Dat abase", "database open")
Dim Param1 As System.Data.Ole Db.OleDbParamet er
Dim Param2 As System.Data.Ole Db.OleDbParamet er
Param1 = objCMD.Paramete rs.Add("@UserNa me",
TextBox1.Text.T rim())
Param2 = objCMD.Paramete rs.Add("@Passwo rd",
Password1.Value .Trim())
intNumUsers = objCMD.ExecuteS calar() 'Execute the query
'ERROR THROWN HERE WORKS ON ACCESS BUT NOT SS
Trace.Warn("Dat abase", "No database error occured")
Catch err As Exception
Dim strErrMessage As String
strErrMessage = "Exception thrown function
btnLogin_Click" &
Request.ServerV ariables("SCRIP T_NAME") & "<br>"
Trace.Write("ER ROR", err.Message.ToS tring)
'Response.End()
'EmailException (err.Message, strErrMessage)
Finally
If objConn.State = ConnectionState .Open Then
Try
'Attempt to close the connection
objConn.Close()
Catch err As Exception
'Catch any error that may occur
Dim strErrMessage As String
strErrMessage = "Exception thrown attemping to
close
after checking the user is in admin. File " &
Request.ServerV ariables("SCRIP T_NAME") & "<br>"
EmailException( err.Message, strErrMessage)
End Try
End If
End Try

Nov 19 '05 #3

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

Similar topics

12
10893
by: Parking Meters | last post by:
Today I managed to get the OleDB provider to let me put datasets from an as400 using sql. Great, the provider string I used is: Provider=IBMDA400.DataSource.1;User ID=XXXXX;Password=XXXX;Data Source=nnn.nnn.nnn.nnn;"/> Question:
4
7822
by: NS | last post by:
Hi, I am trying to execute a prepare statement using oledb provider for DB2. The command.Prepare() statement is giving me an exception " No error information available: DB_E_NOCOMMAND(0x80040E0C)." My code is very simple and is working with other oledb provider, like SQL Server and oracle.
0
5296
by: silesius | last post by:
I've been using VS 2003 to develop a webapplication using C#. Today I exported the application to a remote webserver I begun experiencing problems. It's a simple application that retrieves some information from an Access database. It worked fine when I ran on my computer. I use a parameterized query to retrieve some customer specific information. Customer logs in, I store the username they log in with in the session state and on the...
0
2791
by: silesius | last post by:
I've been using VS.NET 2003 to develop a webapplication using C#. Today I exported the application to another webserver I begun experiencing problems. It's a simple application that retrieves some information from an Access database. It worked fine when I ran on my home computer. I use a datagrid witha a dataset and a parameterized query to retrieve some customer specific information. Customer logs in, I store the username they log in...
5
9666
by: petro | last post by:
Hello all, My asp.net web application works on my machine but I get the following error on our test web server, There is only one oracle home on the test server. Does anyone know how to resolve this error? Oracle error occurred, but error message could not be retrieved from Oracle. Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the...
3
9169
by: blue875 | last post by:
When I run this connection, I get a security error. String connectStr = @"Provider=Microsoft.Jet.OLEDB.4.0;" + @"User Id=USER; Password=PASS;" + @"Data Source=\\SERVER\DIRECTORY\Data.mdb;" + @"Jet OLEDB:System Database=\\SERVER\DIRECTORY\SYSTEM.mdw"; connection = new OleDbConnection(connectStr); connection.Open(); // <=== error!
2
1504
by: ram_palavalasa | last post by:
Hi all, I have a assignment of vc++.net and sybase to populate data and generate reports which is the best one? is OLEDB more efficient than ODBC? i think OLEDB itself is a layer on the ODBC..then how it can be? Thanks, RAM
0
2224
by: Tom | last post by:
Looking for some help with stored procedure call issues. Conceptually, I need to pass a data structure as the sole parameter to the Oracle stored procedure. Sounds simple enough....but how? First, looking at this from the stored procedure side, I see a few things the procedure can do: 1) receive a CLOB/BLOB 2) receive a user defined data type 3) receive multiple params, one for each distinct data element
1
4639
by: ErikJL | last post by:
I have a simple webservice that performs a SELECT query against a database, and then an INSERT statement on the same database/table. The problem arises at the time when we create the second OleDB connection object and attempt to Open() it. The connection string is exactly the same for both connection objects. Another intersting thing is that this code works fine when it is used in a Windows Form project. The code snippet: try { //...
0
9879
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10978
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10705
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10330
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9477
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7879
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5716
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4521
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
4113
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.