Ms access query problem [Resolved]

Posted by Allemahesh under Others on 3/26/2014 | Points: 10 | Views : 642 | Status : [Member] [MVP] | Replies : 2
I am new to ms access. I need to pass the parameters to below query from c#

SELECT * from Vehicle
where regn = 'abc'

I have tried this but not working.

SELECT * from Vehicle
where regn = @regn

How to do this in ms access.




Responses

Posted by: Allemahesh on: 3/26/2014 [Member] [MVP] Silver | Points: 25

Up
0
Down

Resolved
Change the query to below:-

SELECT * from Vehicle 
where regn = [@regn]


Allemahesh, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: A2H on: 3/26/2014 [Member] [MVP] Silver | Points: 25

Up
0
Down
Hi,
I would suggested you to use parameterized queries like given below
You need to change your query like given below
SELECT * from Vehicle where regn = ?


Complete Code
 //Get your connection string here
string ConnString = System.Configuration.ConfigurationManager.ConnectionStrings["YourConnectionString"].ConnectionString;
//Create your query here
string SqlString = "SELECT * from Vehicle where regn = ?";
//Initialize the connection
using (OleDbConnection conn = new OleDbConnection(ConnString))
{
//Initialize the command
using (OleDbCommand cmd = new OleDbCommand(SqlString, conn))
{
cmd.CommandType = CommandType.Text;
//Pass the parameter
cmd.Parameters.AddWithValue("regn", txtregn.Text);
conn.Open();
using (OleDbDataReader reader = cmd.ExecuteReader())
{
while (reader.Read())
{
//Read your values here
}
}
}
}


Thanks,
A2H
My Blog

Allemahesh, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response