潇潇秋瑟 发表于 2008-6-13 15:12

怎样将asp中输入的数据写入SQL中

我编写了一个注册模块,有用户名和密码,用的是HTML中的input(text),
但是要把注册的内容写入SQL数据库中的表user中,该怎样写代码

lqw 发表于 2008-6-13 16:22

是在ASP吗?那可以这样子
<%
Set Conn= Server.CreateObject("ADODB.Connection")
Conn.ConnectionString="Provider=SQLServer;uid=数据库用户名;pwd=数据库密码"'连接数据库
Conn.Open'打开数据库

Sub connClose() '定义一个关闭数据库的子函数
  Conn.Close
  Set Conn = nothing
End Sub
%>
<%
                Dim uName:uName=request.Form("txtUname")
                Dim uPwd:webshopid=request.Form("txtPwd")
                if uName="" Then
                 Response.Write("请填写你的用户名!")
                 Response.End
                End if
                if uPwd="" Then
                 Response.Write("请填写密码!")
                 Response.End
                End if
                set rs=server.createobject("adodb.recordset")
                Conn.Open

                rs.open "select * from 用户表 where uName='"&usname&"'",Conn,1,1
               
                if rs.bof or rs.eof then
                        rs.addnew'添加资料
                        rs("用户名字段")=uName
                        rs("密码字段")=uPwd
                        rs.update
                else
                        response.write("用户已存在!")
                end if
                rs.close
                set rs=nothing
                call connClose()

%>
如果是.net的话,需要给input加上runat="server"属性然后在后台里面包含SqlClient命名空间
定义
SqlConnection conn=new SqlConnection("Server=.;uid=sa;pwd=***")
conn.open()
SqlCommand cmd=new SqlCommand("查询字符串")
可能有点拼写错误,不过在VS里面有代码提示,大概就这样子啦,你也可以上网查查看!

页: [1]

编程论坛