当前位置: 移动技术网 > IT编程>开发语言>.net > ASP.NET书籍信息录入实现代码

ASP.NET书籍信息录入实现代码

2017年12月12日  | 移动技术网IT编程  | 我要评论
1、 在数据库中建立一个test数据库,在test数据库中建立一个book_info表。        book_nam

1、 在数据库中建立一个test数据库,在test数据库中建立一个book_info表。
       book_name      varchar(100)
        author               varchar(50)
         press               varchar(50)
       press_date      varchar(20)
         image              varchar(30)

2、 制作一个如下页面:

当单击“插入图书信息”按钮时,将用户的信息保存到book_info表中。注意:封面图片要求先上传到网站根目录下的“upload”文件夹中,再将图片在网站中的相对路径保存到数据库book_info表的image字段中。
布局代码:

<%@ page language="c#" autoeventwireup="true" codefile="default.aspx.cs" inherits="_default" %> 
 
<!doctype html public "-//w3c//dtd xhtml 1.0 transitional//en" "http://www.w3.org/tr/xhtml1/dtd/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
<head runat="server"> 
 <title></title> 
</head> 
<body> 
 <form id="form1" runat="server"> 
  <table> 
   <tr> 
   <td><asp:label id="label1" runat="server" text="书名:"></asp:label></td> 
   <td><asp:textbox id="textbox1" 
    runat="server"></asp:textbox> 
    </td> 
   </tr> 
   <tr> 
    <td><asp:label id="label2" runat="server" text="作者:"></asp:label></td> 
   <td> <asp:textbox id="textbox2" 
     runat="server"></asp:textbox></td> 
    </tr> 
    <tr> 
     <td> <asp:label id="label3" runat="server" text="出版社:"></asp:label></td> 
     <td><asp:dropdownlist id="dropdownlist1" runat="server" autopostback="true"> 
     <asp:listitem>清华大学出版社</asp:listitem> 
     <asp:listitem>机械工业出版社</asp:listitem> 
     <asp:listitem>人民邮电出版社</asp:listitem> 
     <asp:listitem>电子工业出版社</asp:listitem> 
    </asp:dropdownlist></td> 
    </tr> 
    <tr> 
    <td><asp:label id="label4" runat="server" text="出版日期:"></asp:label></td> 
     <td><asp:calendar id="calendar1" runat="server"></asp:calendar></td> 
     </tr> 
     <tr> 
     <td> 
      <asp:label id="label5" runat="server" text="封面图片:"></asp:label></td> 
     <td> 
      <asp:fileupload id="fileupload1" runat="server" /></td> 
     </tr> 
     <tr> 
     <td></td> 
     <td> 
      <asp:button id="button1" runat="server" text="插入图片信息" onclick="button1_click" /></td> 
     </tr> 
    </table> 
 </form> 
 
</body> 
</html> 

cs代码

using system; 
using system.collections.generic; 
using system.linq; 
using system.web; 
using system.web.ui; 
using system.web.ui.webcontrols; 
using system.data.sqlclient; 
 
public partial class _default : system.web.ui.page 
{ 
 protected void page_load(object sender, eventargs e) 
 { 
 
 } 
 protected void button1_click(object sender, eventargs e) 
 { 
  string savepath = server.mappath("~/images/"); 
  if (fileupload1.hasfile) 
  { 
   string filename = fileupload1.filename; 
   savepath += filename; 
   fileupload1.saveas(savepath); 
  } 
  string sql = "data source=a25;initial catalog=test;integrated security=true"; 
  string sqlstr = @"insert into book_info(book_name,author,press,press_date,image) values 
    ('" + textbox1.text + "','" + textbox2.text + "','" + dropdownlist1.selecteditem.text + "','" 
     + calendar1.selecteddate.toshortdatestring() + "','" + fileupload1.filename + "')"; 
  using (sqlconnection conn = new sqlconnection(sql)) 
  { 
   conn.open(); 
   using (sqlcommand cmd = conn.createcommand()) 
   { 
    cmd.commandtext = sqlstr; 
    cmd.executenonquery(); 
   } 
  } 
  response.write("插入成功!"); 
 } 
} 

以上就是本文的全部内容,希望可以给大家一个启发,对大家的学习有所帮助。

如您对本文有疑问或者有任何想说的,请点击进行留言回复,万千网友为您解惑!

相关文章:

验证码:
移动技术网