加连接数据库SQL SERVER的添删改查一个简单事例

加连接数据库SQL SERVER的添删改查一个简单事例,第1张

帮你写了一个:

<%

dim conn,sql,rs,str,

'假如你的sql数据在本机,用户名为sa,密码为123456,已经在上面建好一个名为webdata的数据

str="Provider=sqloledbData Source=127.0.0.1Initial Catalog=WebDataUser Id=saPassword=123456"

Set Conn=Server.CreateObject("Adodb.Connection")

Conn.Open str

'1建表

sql="create table ren ( id int primary key identity(1,1),name varchar(20) not null,Age tinyint)"

conn.execute(sql)

'2.添加数据,添加两条

sql="insert into ren (name,Age) values ('fhoujun',21)"

conn.execute(sql)

sql="insert into ren (name,Age) values ('youName',20)"

conn.execute(sql)

call shuchu

'3.更新数据

sql="update ren set Age=80 where id=2"

conn.execute(sql)

call shuchu

'4.删除数据

sql="delete from ren where id=2"

conn.execute(sql)

call shuchu

'5.删表

sql="DROP TABLE ren"

conn.execute(sql)

'关闭数据库

conn.close

set conn=nothing

'输出表ren的内容

sub shuchu()

sql="select * from ren"

Set Rs=CreateObject("Adodb.Recordset")

Rs.open Sql,Conn,1,1

FN=Rs.Fields.Count

RC=Rs.RecordCount

SI="<table><tr height=25 bgcolor=#cccccc><td></td>"

For n=0 to FN-1

Set Fld=Rs.Fields.Item(n)

SI=SI&"<td align='center'>"&Fld.Name&"</td>"

Set Fld=nothing

Next

SI=SI&"</tr>"

Do While Not(Rs.Eof or Rs.Bof)

Bgcolor="#EFEFEF"

SI=SI&"<tr><td bgcolor=#cccccc><font face='wingdings'>x</font></td>"

For i=0 To FN-1

If Bgcolor="#EFEFEF" Then:Bgcolor="#F5F5F5":Else:Bgcolor="#EFEFEF":End if

SI=SI&"<td bgcolor="&Bgcolor&">"&server.htmlencode(Rs(i))&"</td>"

Next

SI=SI&"</tr>"

Rs.MoveNext

Loop

SI="<tr><td colspan="&FN+1&" align=center>记录数:"&RC

SI=SI&"<hr color='#EFEFEF'></td></tr></table>"

Rs.Close:Set Rs=Nothing

response.write SI:SI=""

end sub

%>

因为我的sql数据库没启动,所以没调试,对回答的内容有问题,或认为不妥,百度消息我。

(在消息的内容里加上问题的网址哦)

我举个例子你就明白了

SqlConnection Conn = new SqlConnection(连接字符串)

Conn.Open()//打开数据库连接

......//执行 *** 作

Conn.Close()//关闭数据库连接


欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/sjk/9967474.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2023-05-03
下一篇2023-05-03

发表评论

登录后才能评论

评论列表(0条)

    保存