Today, I will share with you some of the methods and experiences of using access parametric query.
I hope I will be inspired by everyone. I hope the experts will give me more advice if I have written something wrong.
ASP. NET uses oledbcommand's new oledbparameter to create a parameter goods Query
ASP uses the createparameter method of command to create a parameterized Query
(This method is also used to query SQL stored procedures)
ASP. net c # syntax
Oledbparameter parm = new oledbparameter (name, type, direction, size, value );
(In fact, it has seven reloads. You can see them in vs.net)
Parameters
Name (optional) is a string that represents the name of a parameter object.
Type (optional) long integer value, which specifies the Data Type of the parameter object.
(Optional) direction, which is a long integer value and specifies the parameter object type ..
Size (optional) long integer value. It specifies the maximum length (in characters or bytes) of the parameter value ).
Value (optional) indicates the value of the parameter object.
The following is an example to query the news published by all Tsing in the news table.CopyCode The Code is as follows: SQL = "select * From newss where username =? Order by ID"
// Are query conditions used? Number
Oledbconnection conn = new oledbconnection (connstring );
Oledbcommand cmd = new oledbcommand (SQL, Conn );
Oledbparameter parm = new oledbparameter ("Temp", oledbtype. varchar, 50 );
// You can define temp as a parameter object. oledbtype. varchar is a string with a length of 50.
Parm. Direction = parameterdirection. input;
// Specify its type input parameters
Cmd. Parameters. Add (parm );
Cmd. Parameters ["Temp"]. value = "Tsing ";
// Query Tsing. You can also write it as cmd. Parameters [0].
Conn. open ();
Cmd. executereader ();
ASP VBScript syntax
Set parameter = command. createparameter (name, type, direction, size, value)
Same as above
The following is an example to query the news published by all Tsing in the news table.
------------------------------------------------------Copy codeThe Code is as follows: Set conn = server. Createobject ("ADODB. Connection ")
Conn. connectionstring = connstring
Conn. open ()
Set mycmd = server. Createobject ("ADODB. Command ")
Mycmd. activeconnection = Conn
Mycmd. commandtext = SQL
Mycmd. Prepared = true
Set mypar = mycmd. createparameter ("Temp", 129,1, 50, "Tsing ")
Mycmd. Parameters. append mypar
Set myrs = mycmd. Execute
The method is basically the same as above. asp is different in parameter expression.
129 is adchar, and 1 is the input parameter (in fact, it is the default value)
For details, refer to Microsoft's adovb. Inc:
Copy code The Code is as follows: '---- parameterdirectionenum values ----
Const adparamunknown = 0
Const adparaminput = 1
Const adparamoutput = 2
Const adparaminputoutput = 3
Const adparamreturnvalue = 4
'---- Datatypeenum values ----
Const adempty = 0
Const adtinyint = 16
Const adsmallint = 2
Const adinteger = 3
Const adbigint = 20
Const adunsignedtinyint = 17
Const adunsignedsmallint = 18
Const adunsignedint = 19
Const adunsignedbigint = 21
Const adsingle = 4
Const addouble = 5
Const adcurrency = 6
Const addecimal = 14
Const adnumeric= 131
Const adboolean = 11
Const aderror = 10
Const aduserdefined = 132
Const advariant = 12
Const adidispatch = 9
Const adiunknown = 13
Const adguid = 72
Const addate = 7
Const addbdate = 133
Const addbtime = 134
Const addbtimestamp = 135
Const adbstr = 8
Const adchar = 129
Const advarchar = 200
Const adlongvarchar = 201
Const adwchar= 130
Const advarwchar= 202
Const adlongvarwchar= 203
Const ad binary = 128
Const advarbinary = 204
Const adlongvarbinary = 205
Attach the C # class I wrote and the VBScript function. I hope it will be helpful to you.
Copy code The Code is as follows: using system;
Using system. Data;
Using system. configuration;
Using system. Web;
Using system. Data. oledb;
Namespace acc_select
{
/// <Summary>
/// Accselect abstract description
/// </Summary>
Public class accselect
{
// "Provider = Microsoft. Jet. oledb.4.0; Data Source = D: \ DQ \ db1.mdb"
Private string conn = configurationmanager. connectionstrings ["tsingconnectionstring"]. tostring ();
Public String SQL = string. empty;
Public int T = 4;
Public object v = NULL;
Public accselect ()
{
}
/// <Summary>
/// Constructor to pass the ACC parameter query statement
/// </Summary>
/// <Param name = "strsql"> strsql struct </param>
Public accselect (string strsql)
{
SQL = strsql;
}
/// <Summary>
/// Constructor to pass the ACC parameter query statement
/// </Summary>
/// <Param name = "strsql"> parameter query statement </param>
/// <Param name = "Total"> Number of bytes </param>
Public accselect (string strsql, int total)
{
SQL = strsql;
T = total;
}
/// <Summary>
/// Constructor
/// </Summary>
/// <Param name = "strsql"> parameter query statement </param>
/// <Param name = "Total"> Number of bytes </param>
/// <Param name = "value"> object value </param>
Public accselect (string strsql, int total, object value)
{
SQL = strsql;
T = total;
V = value;
}
/// <Summary>
/// The getodd method returns oledbdatareader
/// </Summary>
/// <Param name = "ODT"> define the oledbtype </param>
/// <Returns> </returns>
Public oledbdatareader getodd (oledbtype ODT)
{
Oledbconnection Conns = new oledbconnection (this. Conn );
Oledbcommand cmd = new oledbcommand (this. SQL, Conns );
Oledbparameter parm = new oledbparameter ("Temp", ODT, this. t );
Parm. Direction = parameterdirection. input;
Cmd. Parameters. Add (parm );
Cmd. Parameters [0]. value = This. V;
Conns. open ();
Oledbdatareader ODA = cmd. executereader ();
Cmd. Dispose ();
Return ODA;
}
String SQL
{
Get
{
Return SQL;
}
Set
{
SQL = value;
}
}
Int t
{
Get
{
Return T;
}
Set
{
T = value;
}
}
Object v
{
Get
{
Return V;
}
Set
{
V = value;
}
}
}
}
// Call Method
// Accselect ACC = new accselect ();
// Acc. SQL = "select * From DTT where d_id =? ";
// Acc. t = 10;
// Acc. V = 1;
// Oledbdatareader ODA = acc. getodd (oledbtype. varchar );
// Repeater1.datasource = ODA;
// Repeater1.databind ();
Copy codeThe Code is as follows: function acc_ SQL (SQL, adotype, adodct, strlong, values)
Dim connstring, mycmd, myrs, Conn
connstring = "provider = Microsoft. jet. oledb.4.0; Data Source = "& server. mappath ("db1.mdb")
set conn = server. createobject ("ADODB. connection ")
Conn. connectionstring = connstring
Conn. open ()
set mycmd = server. createobject ("ADODB. command ")
mycmd. activeconnection = conn
mycmd. commandtext = SQL
mycmd. prepared = true
set mypar = mycmd. createparameter ("Temp", adotype, Dodct, strlong, values)
mycmd. parameters. append mypar
set myrs = mycmd. execute
set acc_ SQL = myrs
end function
'call method
'dim RS
'SQL = "select * from users where id =? Order by ID "
'set rs = acc_ SQL (SQL, 3, 1, 4, 1)
'If not Rs. EOF then
'response. write (RS (1)
'end If