Forum Moderators: open
I want to create some stored queries in MsAccess. I've created the really simple "select * from x where y" queries but I have a few queries that look for elements on the page. Lke this:
Dim MyColumn
MyColumn = request.QueryString("ColumnB")
if ( MyColumn<>"" ) then
MyColumn = replace(MyColumn, "'","''")
end if
"SELECT * FROM Tablex WHERE ColumnA = " & MyColumn & " "
Does anyone know how to write this in MsAccess SQL?
Thanks for any help.
I used parameters to insert the text into queries to protect against injection attacks. This way we do not have to worry about injection attacks escaping out of the query to do harm.
namespace ConsoleApplication21
{
class Program
{
static void Main(string[] args)
{
System.Data.DataSet List = Query("%poker%");
//Loops though the dataset to return all the hostnames
//that match the search results.
for (int i = 0;i <= List.Tables[0].Rows.Count - 1;i++)
{
Console.WriteLine(List.Tables[0].Rows[i]["HostName"].ToString());
}
Console.ReadLine();
}
/// <summary>
/// Used to Query the Access Database
/// </summary>
/// <param name="Search">Search term</param>
/// <returns>a dataset which contains all the search results</returns>
static System.Data.DataSet Query(string Search)
{
System.Data.OleDb.OleDbConnection Connection;
Connection = new System.Data.OleDb.OleDbConnection();
Connection.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=\"Poker Sites.mdb\"";
System.Data.OleDb.OleDbCommand Cmd = new System.Data.OleDb.OleDbCommand();
Cmd.Connection = Connection;
Cmd.CommandType = System.Data.CommandType.Text;
Cmd.CommandText = "Select * from Info where HostName like ?";
//----------------------------------------------------------
//The order of the Parameters matters for Access DB's since
//they are not given names only positions.
//----------------------------------------------------------
System.Data.OleDb.OleDbParameter Parameter;
Parameter = new System.Data.OleDb.OleDbParameter();
Parameter.DbType = System.Data.DbType.String;
Parameter.IsNullable = false;
Parameter.Value = Search;
Cmd.Parameters.Add(Parameter);
System.Data.OleDb.OleDbDataAdapter Adapter;
Adapter = new System.Data.OleDb.OleDbDataAdapter();
Adapter.SelectCommand = Cmd;
System.Data.DataSet ds = new System.Data.DataSet();
Adapter.Fill(ds);
return ds;
}
}
}
Thanks again.
<!--#include file='adovbs.asp' -->
.
.
.
public sub spUpdateClientLogins(byval clientid,byval Loginid, byval Name, byval Login, byval Password)
Set cmd = Server.CreateObject("ADODB.Command")
Set cmd.ActiveConnection = connection
cmd.CommandText = "spUpdateClientLogins"
cmd.CommandType = adCmdStoredProc
if (loginid<>"") then
cmd.Parameters.Append cmd.CreateParameter("@loginid", adInteger,adParamInput,,loginid)
end if
cmd.Parameters.Append cmd.CreateParameter("@clientid", adInteger,adParamInput,,10)
cmd.Parameters.Append cmd.CreateParameter("@Name", adVarChar,adParamInput,50,Name)
cmd.Parameters.Append cmd.CreateParameter("@Login", adVarChar,adParamInput,50,Login)
cmd.Parameters.Append cmd.CreateParameter("@Password", adVarChar,adParamInput,24,Password)
Set RsRecordset = Server.CreateObject("ADODB.Recordset")
RsRecordset.CursorType = adOpenForwardOnly
RsRecordset.CursorLocation = adUseClient
RsRecordset.Open cmd
set spUpdateClientLogins = RsRecordset
set cmd = nothing
end sub