您的位置:首页 > 编程语言 > ASP

ASP 连接数据库并对数据库的一些常用操作

2015-01-16 10:22 477 查看
<span style="font-size:14px;color:#666666;">//配置文件连接数据库

<%
Dim conn
Dim connstr
Dim db
'更改数据库名字
db="sssuth#date\db#zzz#1.mdb"
set conn = Server.CreateObject("ADODB.Connection")
connstr="Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & Server.MapPath(""&db&"")
conn.Open connstr
sub connclose()
conn.close
set conn=nothing
End sub
Sub rsclose()
rs.close
set rs=nothing
End Sub
%></span>

//添加操作

<%@language=VBScript codepage=936 %>

<!--#include file="conn.asp"-->

<!--#include file="logok.asp"-->

<%

dim rs

dim sql

dim count

%>

<script language = "JavaScript">

function CheckForm()

{
if (document.myform.title.value.length == 0) {
alert("新闻标题没有填写.");
document.myform.title.focus();
return false;
}

if (document.myform.user.value.length == 0) {
alert("新闻发布人没有填写");
document.myform.user.focus();
return false;
}

if (document.myform.BigClassName.value.length == 0) {
alert("新闻大类没有选");
document.myform.BigClassName.focus();
return false;
}
if (document.myform.Content.value.length == 0) {
alert("新闻内容没有填写.");
document.myform.title.focus();
return false;
}

return true;

}

</script>

<%

If Request( "Submit" )<>"" Then

set rs=server.createobject("adodb.recordset")

SQL="select * from News where ID is Null"

rs.open SQL,conn,1,3

rs.Addnew

rs("title")=Trim( Request("title"))

rs("description")=Trim( Request("description"))

rs("keywords")=Trim( Request("keywords"))

rs("content")=Trim( Request("content"))

rs("bigclassname")=Trim( Request("bigclassname"))

rs("user")=Trim( Request("user"))

rs("updatetime")=Trim( Request("updatetime"))

rs("ok")=Trim( Request("ok") )

rs.Update

rs.close

Response.Redirect ("news_manage.asp")

end if

%>

<table width="97%" border="0" align="center" cellpadding="3" cellspacing="1" class="tableBorder">

        <form method="POST" name="myform" onSubmit="return CheckForm();" action="" target="_self">

          <tr> 

            <td height="30" colspan="2" align="center" bgcolor="#A4B6D7" class="title">增 加 新 闻 内 容</td>

          </tr>
 <tr> 

            <td class="title2" height="30" colspan="2"  >选择您需要更改的新闻信息。</td>

          </tr>
  

          <tr bgcolor="#ECF5FF"> 

            <td width="124" height="25" align="right" class="table"><font color="#FF0000">*</font>新闻标题:</td>

            <td width="622" class="table"><input name="title" type="text" class="input" size="50" maxlength="200" /></td>

          </tr>

          

          <tr bgcolor="#ECF5FF"> 

            <td width="124" height="25" align="right" class="table">关键字描述:</td>

            <td width="622" class="table"><input name="description" type="text" class="input" size="50" maxlength="200" /></td>

          </tr>          

          

          <tr bgcolor="#ECF5FF"> 

            <td width="124" height="25" align="right" class="table">关键字:</td>

            <td width="622" class="table"><input name="keywords" type="text" class="input" size="50" maxlength="200" /></td>

          </tr>

          

          <tr bgcolor="#ECF5FF"> 

            <td height="25" align="right" class="table"><font color="#FF0000">*</font>新闻类别:</td>

            <td class="table">

<% 

set rs=server.createobject("adodb.recordset")

sql = "select * from news_class"

        rs.open sql,conn,1,1
if rs.eof and rs.bof then
response.write "请先添加栏目。"
else
%> <select name="BigClassName" onChange="changelocation(document.myform.BigClassName.options[document.myform.BigClassName.selectedIndex].value)" size="1">

                <option value="<%=trim(rs("BigClassName"))%>"><%=trim(rs("BigClassName"))%></option>

                <%
dim selclass
   selclass=rs("BigClassName")

        rs.movenext
   do while not rs.eof
%>

                <option value="<%=trim(rs("BigClassName"))%>" selected="selected"><%=trim(rs("BigClassName"))%></option>

                <%
       rs.movenext

       loop
end if

        rs.close
%>

              </select> </td>

          </tr>

          <tr bgcolor="#ECF5FF"> 

            <td height="25" align="right" valign="top" class="table"><font color="#FF0000">*<
4000
/font>新闻内容:</td>

            <td valign="top" class="table"> 

             <textarea name="content" style="width:700px;height:400px;visibility:hidden;"><%=htmlspecialchars(htmlData)%></textarea></td>

          </tr>

         

          <tr bgcolor="#ECF5FF"> 

            <td height="24" align="right" class="table"><font color="#FF0000">*</font>推荐:</td>

            <td valign="top" class="table">

<input type="radio" value="true"name="ok"> 是 

<input name="ok" type="radio" value="false" checked="checked">  

否</td>

          </tr>

          <tr bgcolor="#ECF5FF"> 

            <td height="25" align="right" class="table"><font color="#FF0000">*</font>发布人:</td>

            <td class="table"> <input name="user" type="text" class="input" value="admin" size="30"></td>

          </tr>

          

          <tr> 

            <td height="30" align="right" bgcolor="#ECF5FF" class="table"> 录入时间: </td>

            <td height="30" bgcolor="#ECF5FF" class="table"> 

                <input name="UpdateTime" type="text" id="UpdateTime2" value="<%=date()%>" maxlength="50">当前时间为:<%=date()%> 注意不要改变格式。 </td>

          </tr>

          <tr> 

            <td height="30" colspan="2" align="center" bgcolor="#ECF5FF" class="table"> <input type="submit" name="Submit" value="提交" class="btn">

                

            <input type="reset" name="Submit" value="重置" class="btn"> </td>

          </tr>

        </form>

</table>

//循环查询操作

 <% set new3=server.createobject("adodb.recordset")
sqltext="select  top 1 * from news where BigClassName='行业动态' Order BY ID desc "
new3.open sqltext,conn,1,1
   Do While Not new3.Eof %>

      <dl class="newsdl">

        <dt><a href=""><img src="img/new.jpg" /></a></dt>

        <dd>

          <h3><%=new3("title")%></h3>

          <p> <%=new3("description")%> </p>

          <a href="news.asp?id=<%=new3("id")%>"><img src="img/xx.jpg" /></a> </dd>

      </dl>

      <%
 new3.movenext
 loop

      new3.close
 %>

      <ul class="newlist">

        <% set new4=server.createobject("adodb.recordset")
sqltext="select  top 8 * from news where BigClassName='行业动态' Order BY ID desc "
new4.open sqltext,conn,1,1
   Do While Not new4.Eof %>

        <li> <a href="news.asp?id=<%=new4("id")%>">

        
<% 

        if len(new4("title"))<=20 then 

        response.Write(new4("title")) 

        else 

        response.Write(left(new4("title"),14)&"...") 

        end if
%>

       

        

        </a> </li>

        <%
 new4.movenext
 loop

      new4.close
 %>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: