How to pass parameters with Other Databases(i.e. Excel/Ms-Access).


Today i want to discuss an article how to pass parameters to retrive the dataset or datatable or result for other databases such as Ms-Access e.t.c. The article will help all the asp.net programers who wants to retrive the records from Ms-Access,Excel databases.

I have seen in so many forum question in Dotnetspider.I repeatedly saw a question in the forum that there is an error in the execution of the code while passing the parameters when they are using as backend Ms-Access/Ms-Excel.So how to pass parameters from the front end for other databases i will illustrate with snippet of code lines.



using System.Data;
using System.Data.sqlcient;

Oledbconnection con =new Oledbconnection("PROVIDER=Microsoft.Jet.Oledb.4.0";DATA SOURCE=C:\Authors.mdb");
strselect ="Select phone from Authors where Au_fname=? and au_lname=?"
Oledbcommand cmdselect= new Oledbcommand(strselect);
cmdselect.parameters.add("@firstname",txtFirstName.text);
cmdselect.parameters.add("@lastname",txtLastName.text);
con.open();
lblPhone.text =cmdselect.ExecuteScalar();
con.close();


Using Like Operator

string queryString = "SELECT * FROM Table1 WHERE Field1 LIKE ?";
OleDbCommand command = new OleDbCommand(queryString, connection);
command.Parameters.Add("@p1", OleDbType.Char, 3).Value = "a";
OleDbDataReader reader = command.ExecuteReader();


Note :Now that you are using a ? character to represent the parameter instead of using a named parameter


Article by srirama
A Good advice from parent to a Child , Master to a Student , Scholar to an Ignorant is like a doctor prescribed pill it is bitter to take but when they take it will do all good for them --- Bhushan

Follow srirama or read 73 articles authored by srirama

Comments

No responses found. Be the first to comment...


  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:
    Email: