views:

319

answers:

3

Hi I am new to ms access..Can anybody help me in sending parameters to queries and get back the data(with an example)..i am using C# as coding language(Asp.Net)

Thanks in advance

+2  A: 

You will need the following using statement:

using System.Data.OleDb;

Example:

string ConnString = "Microsoft.Jet.OleDb.4.0;Data Source=|DataDirectory|Northwind.mdb";
string SqlString = @"Select * From Contacts 
                    Where FirstName = @FirstName And LastName = @LastName";
using (OleDbConnection conn = new OleDbConnection(ConnString))
{
    using (OleDbCommand cmd = new OleDbCommand(SqlString, conn))
    {
        cmd.CommandType = CommandType.Text;
        cmd.Parameters.AddWithValue("FirstName", txtFirstName.Text);
        cmd.Parameters.AddWithValue("LastName", txtLastName.Text);
        conn.Open();
        using (OleDbDataReader reader = cmd.ExecuteReader())
        {
            while (reader.Read())
            {
                Response.Write(reader["FirstName"].ToString() + " " + reader["LastName"].ToString());
            }
        }
    }
}
Robert Harvey
+1  A: 

Check out this example.

JP Alioto
+1  A: 

ASP.NET provides an AccessDataSource control that greatly simplifies this. You can use it as in the example below and bind the results directly to a gridview.

<asp:AccessDataSource
  id="InvoiceAccessDataSource"
  DataFile="~/App_Data/Northwind.mdb"
  runat="server"
  SelectCommand="[Employee Sales By Country]"
  SelectCommandType="StoredProcedure">
  <SelectParameters>
    <asp:Parameter Name="Beginning Date" Type="DateTime" defaultValue="1/1/1997" />
    <asp:Parameter Name="Ending Date" Type="DateTime" defaultValue="1/31/1997" />
  </SelectParameters>
</asp:AccessDataSource>

<asp:GridView
  id="InvoiceGridView"
  runat="server"
  AutoGenerateColumns="True"
  DataSourceid="InvoiceAccessDataSource" />
ichiban