SQL語句引數化(1)插入資料

2021-06-15 21:05:32 字數 2818 閱讀 8577

webform1.aspx

imports system.text

public class webform1

inherits system.web.ui.page

#region " web 窗體設計器生成的** "

'該呼叫是 web 窗體設計器所必需的。

private sub initializecomponent()

end sub

protected withevents textbox1 as system.web.ui.webcontrols.textbox

protected withevents textbox2 as system.web.ui.webcontrols.textbox

protected withevents textbox3 as system.web.ui.webcontrols.textbox

protected withevents textbox4 as system.web.ui.webcontrols.textbox

protected withevents textbox5 as system.web.ui.webcontrols.textbox

protected withevents textbox6 as system.web.ui.webcontrols.textbox

protected withevents button1 as system.web.ui.webcontrols.button

'注意: 以下佔位符宣告是 web 窗體設計器所必需的。

'不要刪除或移動它。

private designerplaceholderdeclaration as system.object

private sub page_init(byval sender as system.object, byval e as system.eventargs) handles mybase.init

'codegen: 此方法呼叫是 web 窗體設計器所必需的

'不要使用**編輯器修改它。

initializecomponent()

end sub

#end region

dim myconn as new conn

dim mydata as new getdata

dim sql as stringbuilder = new stringbuilder

private sub page_load(byval sender as system.object, byval e as system.eventargs) handles mybase.load

mydata.myconn = new sqlclient.sqlconnection

mydata.myconn.connectionstring = myconn.stringconnection

if not page.ispostback then

end if

end sub

private sub button1_click(byval sender as system.object, byval e as system.eventargs) handles button1.click

dim mypt as sqlclient.sqlparameter() = new sqlclient.sqlparameter()

mypt(0).value = textbox1.text

mypt(1).value = textbox2.text

mypt(2).value = textbox3.text

mypt(3).value = textbox4.text

mypt(4).value = textbox5.text

mypt(5).value = textbox6.text

mydata.myconn.open()

mydata.executesql(sql.tostring, mypt)

' mydata.executesql(sql.tostring, nothing) '不需要用到引數時,傳nothing

mydata.myconn.close()

end sub

end class

getdata.vb

public class getdata

public myconn as sqlclient.sqlconnection

public function executesql(byval sql as string, byval mypt as sqlclient.sqlparameter())

dim mycmd as sqlclient.sqlcommand

mycmd = new sqlclient.sqlcommand(sql, myconn)               ' 儲存過程時sql應改為儲存過程名

' mycmd.commandtype = commandtype.storedprocedure ' 儲存過程時需要新增該語句     

if isnothing(mypt) then

mycmd.executenonquery()

else

dim mypt1 as sqlclient.sqlparameter

for each mypt1 in mypt

mycmd.parameters.add(mypt1)

next

mycmd.executenonquery()

mycmd.parameters.clear()

end if

end function

end class 

SQL語句引數化 封裝

封裝sql語句引數化 以下 寫為sqlcanshuhua.py檔案 encoding utf 8 from pymysql import class mysqlhelper def init self,user,passwd,db,host localhost port 3306,charset u...

Mysql批量插入資料sql語句

假定我們的表結構如下 create table example example id int not null,name varchar 50 not null,value varchar 50 not null,other value varchar 50 not null 通常情況下單條插入的s...

SQL 電力搶修工程插入資料語句

salvaging insert into salvaging prj num,prj name,start date,end date,prj status values 20100015 220kv清經線接地箱及接地線被盜搶修 10 12 2010 00 00 00 10 13 2010 00 ...