zoukankan      html  css  js  c++  java
  • 参数化Sql向数据库插入一条数据

     1 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
     2 
     3 <html xmlns="http://www.w3.org/1999/xhtml">
     4 <head>
     5     <title></title>
     6     <script type="text/javascript">
     7         function gel(id) {
     8             return document.getElementById(id);
     9         }
    10         function DoAdd() {
    11 //            var uName = gel("txtName").value;
    12 //            var uPwd = gel("txtPwd").value;
    13 //            var uCName = gel("txtCNName").value;
    14 //            var uCNo = gel("txtClassNo").value;
    15             //判断数据是否符合要求
    16             //gel("myForm").action = "";
    17             gel("myForm").submit();
    18         }
    19     </script>
    20 </head>
    21 <body>
    22 <center>
    23 <form  id = "myForm" method="post" action="Add.ashx">
    24     <table style=" 100%;">
    25         <tr>
    26             <td>
    27                CilentID:
    28             </td>
    29             <td>
    30                 <input type="text" id="txtID" name="txtID"/>
    31             </td>
    32         </tr>
    33         <tr>
    34             <td>
    35                ClientName:
    36             </td>
    37             <td>
    38                 <input  type="text"  id="txtName" name ="txtName"/>
    39             </td>
    40         </tr>
    41          <tr>   
    42             <td>
    43                AddressStr:
    44             </td>
    45             <td>
    46                 <input  type="text" id="txtAddr" name="txtAddr"/>  
    47             </td>
    48         </tr>
    49         <tr>
    50             <td>
    51                 PostCode:
    52             </td>
    53             <td>
    54                 <input  type="text" id="txtPost"  name="txtPost"/>
    55             </td>
    56         </tr>
    57         <tr>
    58             <td>
    59                 Telphone:
    60             </td>
    61             <td>
    62                 <input  type="text"  id="txtPhone" name="txtPhone"/>
    63             </td>
    64         </tr>
    65         <tr>
    66             <td>
    67                 Email:
    68             </td>
    69             <td>
    70                 <input  type="text" id="txtEmail" name="txtEmail"/>  
    71             </td>
    72         </tr>
    73         <tr>
    74             <td colspan="2">
    75                 <input type="submit" onclick="DoAdd()" value="提交" />
    76             </td>
    77         </tr>
    78        
    79     </table>
    80     </form>
    81     </center>
    82 </body>
    83 </html>

    后台.ashx文件代码

     1 <%@ WebHandler Language="C#" Class="Add" %>
     2 
     3 using System;
     4 using System.Web;
     5 
     6 using System.Data;
     7 using System.Data.SqlClient;
     8 
     9 
    10 public class Add : IHttpHandler {
    11     
    12     public void ProcessRequest (HttpContext context) {
    13         //context.Response.ContentType = "text/plain";
    14         //context.Response.Write("Hello World");
    15         //int  ClientID = Convert.ToInt32(context.Request.Form["txtID"]);
    16         int result = 0;
    17         string ClientName = context.Request.Form["txtName"];
    18         string AddressStr = context.Request.Form["txtAddr"];
    19         string PostCode = context.Request.Form["txtPost"];
    20         string TelPhone = context.Request.Form["txtPhone"];
    21         string Email = context.Request.Form["txtEmail"];
    22         //context.Response.Write("11"+ClientName+AddressStr+PostCode+TelPhone+Email);
    23         
    24         //链接数据库
    25         string connectionStr = "Data Source=PC--20130405SCI\\YAOSIR;Initial Catalog=Clients;User ID= sa;Password=123456";
    26 
    27         try
    28         {
    29             SqlConnection conn = new SqlConnection(connectionStr);
    30             conn.Open();
    31             string sqlStr = "insert into OrderClient(ClientName,AddressStr,PostCode,Telephone,Email) values(@ClientName,@AddressStr,@PostCode,@Telephone,@Email)";
    32             SqlCommand cmd = new SqlCommand(sqlStr, conn);
    33             SqlParameter[] parames = { 
    34                                 // new SqlParameter("@id",ClientID),
    35         new SqlParameter("@ClientName",ClientName),
    36         new SqlParameter("@AddressStr",AddressStr),
    37         new SqlParameter("@PostCode",PostCode),
    38         new SqlParameter("@Telephone",TelPhone),
    39         new SqlParameter("@Email",Email)
    40                                  };
    41 
    42             cmd.Parameters.AddRange(parames);
    43 
    44             result = cmd.ExecuteNonQuery();
    45         }
    46         catch (Exception)
    47         {
    48 
    49             throw;
    50         }
    51         if (result != 0)
    52         {
    53             context.Response.Write("插入成功!");
    54         }
    55         else
    56         {
    57             context.Response.Write("插入失败!");
    58         }
    59     }
    60  
    61     public bool IsReusable {
    62         get {
    63             return false;
    64         }
    65     }
    66 
    67 }

     注意Table要位于form表单内才能提交数据到服务器端交给.ashx处理,要提交的数据要包含name属性!

  • 相关阅读:
    themes、skins
    使用GreyBox实现Ajax模式窗口
    .net最小化到系统托盘
    asp.net自定义控件
    [转]SQL函数的简短说明
    prototype1.4 和1.5
    [转]Oracle PL/SQL 编程手册(SQL大全)
    更新同一张表中的数据的方法
    js中eval()的作用
    asp.net中的中文和特殊字符的处理方式!
  • 原文地址:https://www.cnblogs.com/yaoxc/p/3100081.html
Copyright © 2011-2022 走看看