ACCESS的參數(shù)化查詢-Access數(shù)據(jù)庫教程
時間:2009-09-16 08:42 來源:編程教程_學(xué)習(xí)大 作者:whl71060… 閱讀:次
問及access被注入的安全問題
許多人處理的方法仍然是用Replace替換特殊字符,然而這樣做也并沒有起到太大做用
今天我就把我用access參數(shù)化查詢的一些方法和經(jīng)驗(yàn)和大家分享
希望對大家有所啟發(fā),有寫的不對的地方希望高手們多多指教
ASP.NET 用OleDbCommand的new OleDbParameter創(chuàng)建參數(shù)貨查詢
ASP用Command的CreateParameter 方法創(chuàng)建參數(shù)化查詢
(SQL儲存過程查詢也是用這個方法建立的)
ASP.NET C#語法:
OleDbParameter parm = new OleDbParameter(Name, Type, Direction, Size, Value);
(實(shí)際上它有七重載大家具體大家能夠在VS.net里面就能夠看到)
參數(shù)
Name 可選,字符串,代表 Parameter 對象名稱。
Type 可選,長整型值,指定 Parameter 對象數(shù)據(jù)類型。
Direction 可選,長整型值,指定 Parameter 對象類型。。
Size 可選,長整型值,指定參數(shù)值最大長度(以字符或字節(jié)數(shù)為單位)。
Value 可選,變體型,指定 Parameter 對象的值。
以下是實(shí)例,查詢news表中所有tsing發(fā)表的舊事
-------------------------------------------------------
sql="select * from newss where username=? order by id"
//注意查詢的條件均用?號表示
OleDbConnection conn = new OleDbConnection(connString);
OleDbCommand cmd = new OleDbCommand(sql,conn);
OleDbParameter parm = new OleDbParameter("temp",OleDbType.VarChar, 50);
//temp為Parameter對象可隨便定義,OleDbType.VarChar指定為字符串,長度50
parm.Direction = ParameterDirection.Input;
//指定其類型輸入?yún)?shù)
cmd.Parameters.Add(parm);
cmd.Parameters["temp"].Value = "tsing";
//查詢tsing,也能夠?qū)懗蒫md.Parameters[0]
conn.Open();
cmd.ExecuteReader();
ASP VBSCRIPT語法
Set parameter = command.CreateParameter (Name, Type, Direction, Size, Value)
參數(shù)同上
以下是實(shí)例,查詢news表中所有tsing發(fā)表的舊事
------------------------------------------------------
et 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
與上面基本相同不同的地方法是asp在對參數(shù)的表達(dá)上面不同
129為adChar,1就是指示輸入?yún)?shù)(是其實(shí)是默認(rèn)值)
大家請參閱MICROSOFT的ADOVB.Inc:
’---- 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 adBinary = 128
Const adVarBinary = 204
Const adLongVarBinary = 205
附我寫的C#類,和VBSCRIPT函數(shù),希望對大家有協(xié)助
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Data.OleDb;
namespace acc_select
{
/// <summary>
/// accselect 的摘要說明
/// </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>
/// 構(gòu)造函數(shù),傳送ACC參數(shù)查詢語句
/// </summary>
/// <param name="strsql">strsql字符型</param>
public accselect(string strsql)
{
sql = strsql;
}
/// <summary>
/// 構(gòu)造函數(shù),傳送ACC參數(shù)查詢語句
/// </summary>
/// <param name="strsql">參數(shù)查詢語句</param>
/// <param name="total">字節(jié)數(shù)</param>
public accselect(string strsql, int total)
{
sql = strsql;
t = total;
}
/// <summary>
/// 構(gòu)造函數(shù)
/// </summary>
/// <param name="strsql">參數(shù)查詢語句</param>
/// <param name="total">字節(jié)數(shù)</param>
/// <param name="value">OBJECT值</param>
public accselect(string strsql, int total, object value)
{
sql = strsql;
t = total;
v = value;
}
/// <summary>
/// getOdd方法前往OleDbDataReader
/// </summary>
/// <param name="odt">定義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;
}
}
}
}
//調(diào)用方法
//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();
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,adodct,strlong,values)
mycmd.Parameters.Append mypar
set myrs = mycmd.Execute
set acc_sql=myrs
end function
’調(diào)用方法
’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
許多人處理的方法仍然是用Replace替換特殊字符,然而這樣做也并沒有起到太大做用
今天我就把我用access參數(shù)化查詢的一些方法和經(jīng)驗(yàn)和大家分享
希望對大家有所啟發(fā),有寫的不對的地方希望高手們多多指教
ASP.NET 用OleDbCommand的new OleDbParameter創(chuàng)建參數(shù)貨查詢
ASP用Command的CreateParameter 方法創(chuàng)建參數(shù)化查詢
(SQL儲存過程查詢也是用這個方法建立的)
ASP.NET C#語法:
OleDbParameter parm = new OleDbParameter(Name, Type, Direction, Size, Value);
(實(shí)際上它有七重載大家具體大家能夠在VS.net里面就能夠看到)
參數(shù)
Name 可選,字符串,代表 Parameter 對象名稱。
Type 可選,長整型值,指定 Parameter 對象數(shù)據(jù)類型。
Direction 可選,長整型值,指定 Parameter 對象類型。。
Size 可選,長整型值,指定參數(shù)值最大長度(以字符或字節(jié)數(shù)為單位)。
Value 可選,變體型,指定 Parameter 對象的值。
以下是實(shí)例,查詢news表中所有tsing發(fā)表的舊事
-------------------------------------------------------
sql="select * from newss where username=? order by id"
//注意查詢的條件均用?號表示
OleDbConnection conn = new OleDbConnection(connString);
OleDbCommand cmd = new OleDbCommand(sql,conn);
OleDbParameter parm = new OleDbParameter("temp",OleDbType.VarChar, 50);
//temp為Parameter對象可隨便定義,OleDbType.VarChar指定為字符串,長度50
parm.Direction = ParameterDirection.Input;
//指定其類型輸入?yún)?shù)
cmd.Parameters.Add(parm);
cmd.Parameters["temp"].Value = "tsing";
//查詢tsing,也能夠?qū)懗蒫md.Parameters[0]
conn.Open();
cmd.ExecuteReader();
ASP VBSCRIPT語法
Set parameter = command.CreateParameter (Name, Type, Direction, Size, Value)
參數(shù)同上
以下是實(shí)例,查詢news表中所有tsing發(fā)表的舊事
------------------------------------------------------
et 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
與上面基本相同不同的地方法是asp在對參數(shù)的表達(dá)上面不同
129為adChar,1就是指示輸入?yún)?shù)(是其實(shí)是默認(rèn)值)
大家請參閱MICROSOFT的ADOVB.Inc:
’---- 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 adBinary = 128
Const adVarBinary = 204
Const adLongVarBinary = 205
附我寫的C#類,和VBSCRIPT函數(shù),希望對大家有協(xié)助
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Data.OleDb;
namespace acc_select
{
/// <summary>
/// accselect 的摘要說明
/// </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>
/// 構(gòu)造函數(shù),傳送ACC參數(shù)查詢語句
/// </summary>
/// <param name="strsql">strsql字符型</param>
public accselect(string strsql)
{
sql = strsql;
}
/// <summary>
/// 構(gòu)造函數(shù),傳送ACC參數(shù)查詢語句
/// </summary>
/// <param name="strsql">參數(shù)查詢語句</param>
/// <param name="total">字節(jié)數(shù)</param>
public accselect(string strsql, int total)
{
sql = strsql;
t = total;
}
/// <summary>
/// 構(gòu)造函數(shù)
/// </summary>
/// <param name="strsql">參數(shù)查詢語句</param>
/// <param name="total">字節(jié)數(shù)</param>
/// <param name="value">OBJECT值</param>
public accselect(string strsql, int total, object value)
{
sql = strsql;
t = total;
v = value;
}
/// <summary>
/// getOdd方法前往OleDbDataReader
/// </summary>
/// <param name="odt">定義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;
}
}
}
}
//調(diào)用方法
//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();
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,adodct,strlong,values)
mycmd.Parameters.Append mypar
set myrs = mycmd.Execute
set acc_sql=myrs
end function
’調(diào)用方法
’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
(責(zé)任編輯:admin)
頂一下
(0)
0%
踩一下
(1)
100%
相關(guān)內(nèi)容
- ·sql語句中l(wèi)ike通配符的匯總(*?!#-等含
- ·Access常用sql基本查詢語句匯總
- ·SQL查詢語句的一般格式小結(jié)
- ·聯(lián)合查詢應(yīng)用舉例1
- ·如何取消操作查詢的提示
- ·報(bào)表中先按組再按條件統(tǒng)計(jì)
- ·測試查詢速度
- ·條件選擇列求和
- ·SQL必知必會(14)NOT操作符
- ·ACCESS的參數(shù)化查詢-Access數(shù)據(jù)庫教程
- ·ACCESS的真假:四、"SELECT * INTO 工
- ·在Access中利用搜索窗體中的值生成動態(tài)
- ·Access使用查詢
- ·隨機(jī)得到Access數(shù)據(jù)庫記錄
- ·SQL 高級使用
- ·交叉表查詢中的累計(jì)
最新內(nèi)容
推薦內(nèi)容
熱點(diǎn)內(nèi)容
- ·Sql Server 和 Access 操作數(shù)據(jù)庫結(jié)構(gòu)的常
- ·ACCESS中使用SQL語句應(yīng)注意的地方及幾點(diǎn)技
- ·一個計(jì)算庫存及結(jié)轉(zhuǎn)的例子
- ·如何實(shí)現(xiàn)動態(tài)查詢余額
- ·如何用sql語句添加刪除主鍵?
- ·SQL中語法錯誤(操作符丟失)問題
- ·用Between 和 iif 實(shí)現(xiàn)靈活的查詢
- ·如何提取分組取前N條的記錄
- ·DELPHI中操作ACCESS數(shù)據(jù)庫
- ·Partition,讓你的分組統(tǒng)計(jì)更加得心應(yīng)手