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