首页 > 编程 > .NET > 正文

ADO.NET对象的构造(7)_OleDbParameter(下)

2024-07-10 13:03:16
字体:
来源:转载
供稿:网友

n overloads public overridable function add(byval value as object) as integer implements ilist.add

n overloads public function add(byval parametername as string,byval value as object) as oledbparameter

n overloads public function add(byval value as oledbparameter) as oledbparameter

n overloads public function add(byval parametername as string,byval oledbtype as oledbtype) as oledbparameter

n overloads public function add(byval parametername as string,byval oledbtype as oledbtype,byval size as integer) as oledbparameter

n overloads public function add(byval parametername as string,byval oledbtype as oledbtype,byval size as integer,byval sourcecolumn as string) as oledbparameter




示例

public sub createoledbparamcoll(myconn as oledbconnection)

dim mycommand as oledbcommand = new oledbcommand("select * from customers where customerid = ?", myconn)

dim myparamcollection as oledbparametercollection = mycommand.parameters



dim myparm as object = new oledbparameter("customerid", oledbtype.varchar)

dim pindex as integer = myparamcollection.add(myparm)



dim myparm as oledbparameter = myparamcollection.add(new oledbparameter("customerid", oledbtype.varchar))



dim myparm as oledbparameter = myparamcollection.add("customerid", “customeridvalue”);



dim myparm as oledbparameter = myparamcollection.add("customerid", oledbtype.varchar)



dim myparm as oledbparameter = myparamcollection.add("customerid", oledbtype.varchar, 5)



dim myparm as oledbparameter = myparamcollection.add("customerid", oledbtype.varchar, 5, "customerid")



end sub



示例

public shared function createcustomeradapter(conn as oledbconnection) as oledbdataadapter

dim da as oledbdataadapter = new oledbdataadapter()

dim cmd as oledbcommand

dim parm as oledbparameter



' create the selectcommand.

cmd = new oledbcommand("select * from customers where country = @country and city = @city", conn)

cmd.parameters.add("@country", oledbtype.varchar, 15)

cmd.parameters.add("@city", oledbtype.varchar, 15)

da.selectcommand = cmd



' create the insertcommand.

cmd = new oledbcommand("insert into customers (customerid, companyname) values (@customerid, @companyname)", conn)

cmd.parameters.add("@customerid", oledbtype.char, 5, "customerid")

cmd.parameters.add("@companyname", oledbtype.varchar, 40, "companyname")

da.insertcommand = cmd



' create the deletecommand.

cmd = new oledbcommand("delete from customers where customerid = @customerid", conn)

parm = cmd.parameters.add("@customerid", oledbtype.char, 5, "customerid")

parm.sourceversion = datarowversion.original

da.deletecommand = cmd



' create the updatecommand.

cmd = new oledbcommand("update customers set customerid = @customerid, companyname = @companyname where customerid = @oldcustomerid", conn)

cmd.parameters.add("@customerid", oledbtype.char, 5, "customerid")

cmd.parameters.add("@companyname", oledbtype.varchar, 40, "companyname")

parm = cmd.parameters.add("@oldcustomerid", oledbtype.char, 5, "customerid")

parm.sourceversion = datarowversion.original

da.updatecommand = cmd



return da

end function



以下两行

parm = cmd.parameters.add("@customerid", oledbtype.char, 5, "customerid")

parm.sourceversion = datarowversion.original

可以缩写为

cmd.parameters.add("@customerid", oledbtype.char, 5, "customerid").sourceversion = datarowversion.original

类似地当添加参数后马上进行设置参数的值时,也可以进行类似以下缩写

mydataadapter.selectcommand.parameters.add("@categoryname", oledbtype.varchar, 80).value = "toasters"

mydataadapter.selectcommand.parameters.add("@serialnum", oledbtype.integer).value = 239

(信息整理来自msdn)

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表