欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

asp.net连接数据库 增加,修改,删除,查询代码

程序员文章站 2024-03-09 08:24:17
复制代码 代码如下:'数据库连接 public sub connectiondb() try serverurl = readfromini(my.application....
复制代码 代码如下:

'数据库连接
public sub connectiondb()
try

serverurl = readfromini(my.application.info.directorypath & "\config.dll", "service information", "ipaddress")

serverid = readfromini(my.application.info.directorypath & "\config.dll", "service information", "password")
servername = readfromini(my.application.info.directorypath & "\config.dll", "service information", "userid")
serverdatabase = readfromini(my.application.info.directorypath & "\config.dll", "service information", "databasename")
if serverid <> "" then
connectionsqlstring = "server =" + serverurl + ";database=" + serverdatabase + ";uid =" + servername + ";pwd=" + serverid + ";max pool size=500"
else
connectionsqlstring = "server =" & serverurl & ";integrated security = sspi ;database = " & serverdatabase & ""
end if
consql = new sqlconnection(connectionsqlstring)
objcommand.connection = consql
catch ex as exception
msgbox(ex.message)
end try
end sub


'数据操作执行
public sub getconn(byval sqlstr as string, byval tablename as string)
try
objcommand.commandtext = sqlstr
objdataset.clear()
objdataadapter.selectcommand = objcommand
objdataadapter.fill(objdataset, tablename)
catch ex as exception
errno = 1
msgbox(ex.message)
end try
end sub
'数据更新
public sub updatetable(byval strsql as string)
objcommand.commandtext = strsql
try
consql.open()
trans = consql.begintransaction
objcommand.transaction = trans
objcommand.executenonquery()
trans.commit()
catch ese as exception
msgbox(ese.message)
trans.rollback() '如果更新异常则取消所有更新
finally
consql.close() '关闭连接
end try
end sub