Re: SQL CE 2.0 parameterised queries
- From: "William Vaughn \(MVP\)" <billva@xxxxxxxxxxxxxxx>
- Date: Sat, 24 Jan 2009 20:14:37 -0800
Ah, no.
The way Parameters are managed is a function of the .NET Data Provider. If you use OleDB to access any data source, you must (as Alberto says) use ? placeholders. However, if you use the .NET provider (SqlCe) you can (and must) use named Parameters--as shown in this example.
Imports System.Data.SqlServerCe
Public Class Form1
Dim cn As SqlCeConnection
Dim cmd As SqlCeCommand
Dim tb As New DataTable
Dim sqlRS As SqlCeResultSet
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Try
cn = New SqlCeConnection(My.Settings.NorthwindConnectionString)
cn.Open()
cmd = New SqlCeCommand("SELECT [Last Name], [First Name], City" _
& " FROM Employees" _
& " WHERE Region = @RegionWanted", cn)
cmd.Parameters.AddWithValue("@RegionWanted", "WA")
sqlRS = cmd.ExecuteResultSet(ResultSetOptions.Scrollable + ResultSetOptions.Updatable)
DataGridView1.DataSource = sqlRS
Catch ex As Exception
MsgBox(ex.ToString)
End Try
End Sub
End Class
Hth
--
__________________________________________________________________________
William R. Vaughn
President and Founder Beta V Corporation
Author, Mentor, Dad, Grandpa
Microsoft MVP
(425) 556-9205 (Pacific time)
Hitchhiker's Guide to Visual Studio and SQL Server (7th Edition)
http://betav.com http://betav.com/blog/billva
____________________________________________________________________________________________
"Alberto Silva - Microsoft MVP - Device Application Development" <alberto.silva@xxxxxxxxxxxxxxxxxxxxxxxxxxxxx> wrote in message news:466C3BAC-B27F-4941-A1C7-7401FE519968@xxxxxxxxxxxxxxxx
Hi,.
With SSCE 2.0 you can't use named parameters inside the SQL statement, you must use the interrogation mark instead. When executing the command, you can name the parameters you add, but you must add them by the same order they are referenced in the SQL statement, something like...
string sql = "SELECT * FROM myUsersTable WHERE user_code = ? AND password = ?"
SqlCeConnection cn = new SqlCeConnection("Data Source=\\My Documents\
\Tervia.sdf");
SqlCeCommand cmd = new SqlCeCommand(sql,cn);
cmd.Parameters.Add("@UserCode", sUserCode.ToUpper());
cmd.Parameters.Add("@Password", sPassword.ToUpper());
--
Alberto Silva
www.moving2u.pt - R&D Manager
http://msmvps.com/AlbertoSilva - Blog
Microsoft MVP - Device Application Development
"JaffaB" <jaffa_brown@xxxxxxxxxxx> wrote in message news:cc6a7bd3-f1a2-4e62-83dc-15e1572fb6e2@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxAll,
I am starting to loose the will to live with SQL Server CE. My
latest problem is with paramitised queries. Some references on the
web say to use "?" as parameters, and others say you can follow the
SQL Server 2000 @parameters (which is the option I have gone for to
make it compatible).
However, when I use the parameters with the @ (as per the code below),
I get a "System.Data.SqlServerCe.SqlCeException" error. I havnt tried
the "?" parameters yet.
what am I doing wrong? My code is as follows:
System.Reflection.Assembly asm =
System.Reflection.Assembly.GetExecutingAssembly();
ResourceManager rm = new ResourceManager
("MBTHandheld.SQLCEQuery",asm);
string sql = rm.GetString("VerifyLogon1");
// Sql returned is SELECT active Active, user_type UserType FROM
bt_user WHERE user_code = @UserCode AND password = @Password
SqlCeConnection cn = new SqlCeConnection("Data Source=\\My Documents\
\Tervia.sdf");
SqlCeCommand cmd = new SqlCeCommand(sql,cn);
cmd.Parameters.Add("@UserCode", sUserCode.ToUpper());
cmd.Parameters.Add("@Password", sPassword.ToUpper());
cmd.Connection.Open();
I know the its the parameters causing the problem, becuase without the
parameters (as per the code below), it works:
System.Reflection.Assembly asm =
System.Reflection.Assembly.GetExecutingAssembly();
ResourceManager rm = new ResourceManager
("MBTHandheld.SQLCEQuery",asm);
string sql = rm.GetString("VerifyLogon2");
// Sql returned is SELECT active Active, user_type UserType FROM
bt_user
SqlCeConnection cn = new SqlCeConnection("Data Source=\\My Documents\
\Tervia.sdf");
SqlCeCommand cmd = new SqlCeCommand(sql,cn);
cmd.Connection.Open();
- Follow-Ups:
- Re: SQL CE 2.0 parameterised queries
- From: Alberto Silva - Microsoft MVP - Device Application Development
- Re: SQL CE 2.0 parameterised queries
- References:
- SQL CE 2.0 parameterised queries
- From: JaffaB
- Re: SQL CE 2.0 parameterised queries
- From: Alberto Silva - Microsoft MVP - Device Application Development
- SQL CE 2.0 parameterised queries
- Prev by Date: Should SQL Compact work in headless systems?
- Next by Date: Re: SQL CE 2.0 parameterised queries
- Previous by thread: Re: SQL CE 2.0 parameterised queries
- Next by thread: Re: SQL CE 2.0 parameterised queries
- Index(es):
Relevant Pages
|
Loading