更新、插入数据库所使用的UPDATE()

原创 2003年03月17日 08:59:00

<%@ Page Language="C#" EnableSessionState="False" %>
<%@ Import Namespace="System" %>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.OleDb" %>
<%@ Import Namespace="System.Text" %>
<%@ Import Namespace="System.IO" %>

<html>
<head>
<title>谢谢你的留意!在听宁信息!^_^</title>
<script language="C#" runat="server" codepage="936">
 
  void Page_Load(Object Src, EventArgs E)
  {
     //Check id the page is loaded for the first time
     if (!Page.IsPostBack) {
       //Get the Parameters from the Query string and store it
       string name = Request.Params["name"] ;
       string email = Request.Params["email"] ;
       string subject = Request.Params["subject"] ;
       string ip = Request.Params["ip"] ;
       string date = Request.Params["date" ];
       string message = Request.Params["message"] ;
       bool newmess =true ;
       string previd ="1";
       //Check of the 'newpost' paramater is 'no'
       //indicating that its a reply to a previous post
       if(Request.Params["newpost"].Equals("no"))
       {
          newmess =false ;
          //Since its a reply, we get the ID of the topic
          //to which this post is a reply
          previd = Request.Params["previd"] ;
       }
      
       if(newmess)
       {
          //Execute the code below to insert a new topic
          string
strConn=@"Provider=Microsoft.Jet.OleDb.4.0 ;Data Source=";
                 strConn+=Server.MapPath(".//db//board.mdb") ;
         
          OleDbConnection myConn = new OleDbConnection(strConn) ;
          //SQL query with Parameters
         string insertStr =" INSERT INTO newpost (name, email, subject, ip, dt, message) VALUES ";
           insertStr+="(@name, @email, @subject, @ip, @dt, @message)";
          //Create a new OleDbCommand
          OleDbCommand insertCommand = new OleDbCommand(insertStr, myConn);
          //Add a new Parameter
'@name' of the type 'VarChar'
          //and set its value
          insertCommand.Parameters.Add(new OleDbParameter("@name", OleDbType.VarChar));
          insertCommand.Parameters["@name"].Value = name;
    
          insertCommand.Parameters.Add(new OleDbParameter("@email", OleDbType.VarChar));
          insertCommand.Parameters["@email"].Value = email;

 insertCommand.Parameters.Add(new OleDbParameter("@subject", OleDbType.VarChar));
          insertCommand.Parameters["@subject"].Value = subject;

  insertCommand.Parameters.Add(new OleDbParameter("@ip", OleDbType.VarChar));
          insertCommand.Parameters["@ip"].Value = ip;

  insertCommand.Parameters.Add(new OleDbParameter("@dt", OleDbType.VarChar));
          insertCommand.Parameters["@dt"].Value = date;

         insertCommand.Parameters.Add(new OleDbParameter("@message", OleDbType.VarChar));
          //Give a call the the 'parsetext' method to parse the message
          insertCommand.Parameters["@message"].Value = parsetext(message);

          myConn.Open();
          //Execute Non Query to insert a new topic in the database
          insertCommand.ExecuteNonQuery();
          myConn.Close() ;
        }
        else
        {
           //Insert a reply to a previous topic
           string
strConn=@"Provider=Microsoft.Jet.OleDb.4.0 ;Data Source=";
                  strConn+=Server.MapPath(".//db//board.mdb") ;
           OleDbConnection myConn = new OleDbConnection(strConn);
           //SQL statement with Parameters
           string insertStr =" INSERT INTO reply (name, email, subject, ip, dt, ";
                  insertStr+="message, postid) VALUES ";
                 insertStr+="(@name, @email, @subject, @ip, @dt, @message, @postid)";
  //Create a new OleDbCommand
          OleDbCommand insertCommand = new OleDbCommand(insertStr, myConn);
   //Add a new Parameter and set its value
          insertCommand.Parameters.Add(new OleDbParameter("@name", OleDbType.VarChar));
          insertCommand.Parameters["@name"].Value = name;
          insertCommand.Parameters.Add(new OleDbParameter("@email", OleDbType.VarChar));
          insertCommand.Parameters["@email"].Value = email;
         insertCommand.Parameters.Add(new OleDbParameter("@subject", OleDbType.VarChar));
          insertCommand.Parameters["@subject"].Value = subject;
  insertCommand.Parameters.Add(new OleDbParameter("@ip", OleDbType.VarChar));
          insertCommand.Parameters["@ip"].Value = ip;
  insertCommand.Parameters.Add(new OleDbParameter("@dt", OleDbType.VarChar));
          insertCommand.Parameters["@dt"].Value = date;
       insertCommand.Parameters.Add(new OleDbParameter("@message", OleDbType.VarChar));
           //Give a call the the 'parsetext' method to parse the message
          insertCommand.Parameters["@message"].Value = parsetext(message);
         insertCommand.Parameters.Add(new OleDbParameter("@postid", OleDbType.Integer));
          insertCommand.Parameters["@postid"].Value = previd;
          myConn.Open();
  //Update the Database
          insertCommand.ExecuteNonQuery() ;
          myConn.Close();
          //SQL string to get the 'replies' column of the topic
  //to which this post is a reply
          string replyno = "SELECT replies FROM newpost WHERE postid ="+previd ;
          insertCommand.CommandText =replyno ;
          myConn.Open();
          OleDbDataReader reader =insertCommand.ExecuteReader() ;
          reader.Read();
  //Get the number of replies to this post
          int rep =reader.GetInt16(0) ;
          myConn.Close();
          rep++ ;
          //SQL statement to update the number of replies
  //of the topic to which this post is a reply
          string updtStr ="UPDATE newpost SET replies = "+rep
  +" WHERE (postid = "+previd+")" ;
          insertCommand.CommandText = updtStr;
          myConn.Open();
          //Execute the command
          insertCommand.ExecuteNonQuery();
          myConn.Close() ;
       }
       //Set the text of various textboxes to inform
       //the user of the text entered into the database
       NameLabel.Text = name;
       EmailLabel.Text= email ;
       SubjectLabel.Text=subject;    
       MessageLabel.Text=message ;   
    }
   else
    {
       errmess.Text="This Page Cannot be called directly.";
       errmess.Text+=" It has to be called from the Form posting page.<br>" ;
     }
  }
  //Class to parse the Message into HTML format
  public string parsetext(string text)
  {
  //Create a StringBuilder object from the string input
  //parameter
  StringBuilder sb = new StringBuilder(text) ;
  //Replace all double white spaces with a single white space
  //and &nbsp;
  sb.Replace("  "," &nbsp;");
  //Check if HTML tags are not allowed
 
     //Convert the brackets into HTML equivalents
     sb.Replace("<","&lt;") ;
     sb.Replace(">","&gt;") ;
     //Convert the double quote
     sb.Replace("/"","&quot;");

  //Create a StringReader from the processed string of
  //the StringBuilder
  StringReader sr = new StringReader(sb.ToString());
  StringWriter sw = new StringWriter();
  //Loop while next character exists
  while(sr.Peek()>-1)
  {
    //Read a line from the string and store it to a temp
    //variable
    string temp = sr.ReadLine();
    //write the string with the HTML break tag
    //Note here write method writes to a Internal StringBuilder
    //object created automatically
    sw.Write(temp+"<br>") ;
  }
  //Return the final processed text
  return sw.GetStringBuilder().ToString();
}

</script>
</head>
<body topmargin="0" leftmargin="0" rightmargin="0" marginwidth="0" marginheight="0">

<center>
<asp:label id="errmess" text="" style="color:#FF0000" runat="server" />
<h2 class="fodark"><b>谢谢谢!你在听宁信息城填广场留下你的笔迹!</b></h2>
<table align=center width="60%" border="0" cellspacing="2" cellpadding="1" >
<tr class="fohead"><td colspan="2">你留下以下的信息!谢谢!^_^</td></tr>
<tr class="folight">
<td>名名:</td> 
<td><asp:label id="NameLabel" text="" runat="server" /></td>
</tr>
<tr class="folight">
<td>E-Mail :</td> 
<td><asp:label id="EmailLabel" text="" runat="server" /></td>
</tr>
<tr class="folight">
<td>标题 :</td>
<td><asp:label id="SubjectLabel" text="" runat="server" /></td>
</tr>
<tr class="folight">
<td>信息内容:</td>
<td><asp:label id="MessageLabel" text="" runat="server" /></td>
</tr>
</table>

</center>
</body>
</html>

这里更多的文件

android sqlite 创建数据库,表、增加数据、更新数据、删除数据

今天学习了android 中内嵌的数据库sqlite,sqlite的简单介绍: SQLite简介 Google为Andriod的较大的数据处理提供了SQLite,他在数据存储、管理、维护等各方...
  • sinat_29962405
  • sinat_29962405
  • 2015年12月03日 23:04
  • 4149

Python进阶(三十一)-往MySQL数据库里添加数据,update和insert哪个效率高

Python进阶(三十一)-往MySQL数据库里添加数据,update和insert哪个效率高  在编写“Water Spider”过程中,遇到了批量更新数据的情况。自己刚开始时想使用mysql提供的...
  • sunhuaqiang1
  • sunhuaqiang1
  • 2017年04月06日 16:36
  • 42356

SQL-批量插入和批量更新

最新总是有人问我sql优化的问题,大部分的原因就是更新实体集合或者更新,因为用的是持久化的框架,例如hibernate,EntityFrmwork,所有这里我总结一下如何在数据库中利用sql进行批量的...
  • lovemenghaibin
  • lovemenghaibin
  • 2016年02月28日 10:45
  • 24125

C#使用SqlDataAdapter.Update更新数据库

http://www.cnblogs.com/ChangTan/archive/2011/12/27/2303901.html 此处将资料从数据库得出后显在DataGridVie...
  • wyqlxy
  • wyqlxy
  • 2012年04月11日 13:06
  • 692

批量更新数据库数据(SQL update select语句)

QL update select语句   最常用的update语法是: UPDATE SET = , SET =   www.2cto.com   如果我的更新值Value是从一条...
  • huchen0907
  • huchen0907
  • 2014年10月08日 11:15
  • 5212

oracle数据库:update更新多表总结

总体原则:1)更新的时候一定要加where条件,否则必然引起该字段的所有记录更新                    2)跨表更新时,set和where时,尽量减少扫描次数,从而提高优化 ...
  • funnyfu0101
  • funnyfu0101
  • 2016年10月09日 18:16
  • 3641

SQL Server数据库自增长字段标识列的更新(update)修改终极操作方法

SQL Server数据库自增长标识列的更新修改操作方法 在日常的sql server开发中,经常会用到Identity类型的标识列作为一个表结构的自增长编号。比如文章编号、记录序号等等。自增长的标...
  • yuyu_12345
  • yuyu_12345
  • 2014年01月16日 09:40
  • 2936

3.数据库操作相关术语,Oracle认证,insert into,批量插入,update tablename set,delete和truncate的区别,sql文件导入

1相关术语 语句 含义 操作 DML语句 (Data Manipulation Language) 数据库操作语言 insert up...
  • toto1297488504
  • toto1297488504
  • 2014年10月10日 23:47
  • 1730

3.非关系型数据库(Nosql)之mongodb:升降序排序,排序分页,$all,$in,$nin,$or,$nor, $exists,游标,更新(update,$set,$unset,$inc)

 1排序sort() A降序排列 db.c4.find().sort().sort({age:-1}); B升序排列: ...
  • u012514278
  • u012514278
  • 2017年01月10日 18:07
  • 118

MySQL数据库update更新子查询

比如: 1 2 3 4 UPDATE test.tb_vobile a set a.name = '111 ' ...
  • tomakemyself
  • tomakemyself
  • 2017年07月02日 16:51
  • 383
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:更新、插入数据库所使用的UPDATE()
举报原因:
原因补充:

(最多只允许输入30个字)