Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Parameters in query to mdb using oledb

6 views
Skip to first unread message

Kristian

unread,
Jan 18, 2005, 3:41:01 AM1/18/05
to
Hello,
I'm developing a website with asp .net and want to insert data into a mdb.
As I'm fetching the data from a web form I need to be able to have
parameters in the SQL.
Which sign indicates a parameter in this situation?
oledb objects should mean "%" or what?
Sample code:
cmdIns.CommandText =
"INSERT INTO qPersoner ( Företag, FNamn, ENamn" & _
", Adressrad1, Adressrad2, Pnr, Ort, Tfn1, Tfn2, Epost, " & _
"Medlem, Medlemsnr, Instruktör, Aktiv, Livinfo)" & _
" Values (%,%, %, %, %, %, %, %, %, %, %, %, %, %, %)"

Stuart Rolinson

unread,
Jan 18, 2005, 6:43:04 AM1/18/05
to
I have just done the same thing, this was how I managed it.

Create an access query using SQL as per the example below:-

INSERT INTO tblMyTable ( Field1, Field2, Field3)
VALUES ([pField1], [pField2], [pField3]);

pField1 2 and 3 will now be treated as parameters, the only rules appear to
be that all parameters are mandatory and they must be added in the same order
they are present in the SQL statement.

Hope this helps,
Stuart

Cowboy (Gregory A. Beamer) - MVP

unread,
Jan 18, 2005, 11:45:14 AM1/18/05
to
Two options.

1. Text queries in your .NET code: Use ? for each parameter you wish to
supply. Then add in order. This is the easiest.

2. Create a query in Access proper and use the parameter names. This will
work for newer versions of Access and acts much like a stored procedure.


---

Gregory A. Beamer
MVP; MCP: +I, SE, SD, DBA

***************************
Think Outside the Box!
***************************

Paul Clement

unread,
Jan 18, 2005, 1:24:34 PM1/18/05
to
On Tue, 18 Jan 2005 00:41:01 -0800, "Kristian" <Kris...@discussions.microsoft.com> wrote:

¤ Hello,

The placeholder is a question mark, not a percent sign. Here is an example:

Dim AccessConn As System.Data.OleDb.OleDbConnection
Dim AccessCommand As System.Data.OleDb.OleDbCommand
Dim strValue As String = "This is a test"
AccessConn = New System.Data.OleDb.OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source=e:\My Documents\db1.mdb")

AccessConn.Open()

AccessCommand = New System.Data.OleDb.OleDbCommand
AccessCommand.Connection = AccessConn
AccessCommand.CommandText = "INSERT INTO MyTable(email) VALUES (?)"
AccessCommand.CommandType = CommandType.Text
AccessCommand.Parameters.Add("email", strValue)

AccessCommand.ExecuteNonQuery()

AccessConn.Close()


Paul ~~~ pcle...@ameritech.net
Microsoft MVP (Visual Basic)

Vladimir Sergeyev [MS]

unread,
Jan 19, 2005, 7:00:35 PM1/19/05
to
Regarding #1: this only applies to OleDb and Odbc ADO.Net providers. If
you're using Oracle ADO.Net provider, the placeholders are parameter names
with ":" added. For example:

OracleCommand myCommand = new OracleCommand("inset into MyTable
values(:p1)", myConn);
OracleParameter myParam = new OracleParameter();

myParam.Name = "p1";
myParam.OracleType = OracleType.VarChar;
myParam.Value = "some value";
myCommand.Add(myParam);
myCommand.ExecuteNonQuery();


Note that parameter name that is passed to OracleParameter does not have
":".

--
Vladimir Sergeyev
ADO.Net Test Team
Microsoft Corporation

--------------------------------
This posting is provided "AS IS", with no warranties, and confers no rights.
Please do not send email directly to this alias. This alias is for newsgroup
purposes only.
--------------------------------
"Cowboy (Gregory A. Beamer) - MVP" <NoSpamM...@comcast.netNoSpamM> wrote
in message news:84FDEA70-E98D-409A...@microsoft.com...

Vladimir Sergeyev [MS]

unread,
Jan 19, 2005, 7:54:38 PM1/19/05
to
One correction: "myParam.Name" should be "myParam.ParameterName" ...

--
Vladimir Sergeyev
ADO.Net Test Team
Microsoft Corporation

--------------------------------
This posting is provided "AS IS", with no warranties, and confers no rights.
Please do not send email directly to this alias. This alias is for newsgroup
purposes only.
--------------------------------

"Vladimir Sergeyev [MS]" <vla...@online.microsoft.com> wrote in message
news:ODcjSMo$EHA....@TK2MSFTNGP14.phx.gbl...

0 new messages