AJAX技术对数据库的操作

insertintdatabase.aspx


<%@ Page language="c#" Codebehind="insertintdatabase.aspx.cs" AutoEventWireup="false" Inherits="ajaxstu.insertintdatabase" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
    <HEAD>
        <title>insertintdatabase</title>
        <meta content="Microsoft Visual Studio .NET 7.1" name="GENERATOR">
        <meta content="C#" name="CODE_LANGUAGE">
        <meta content="JavaScript" name="vs_defaultClientScript">
        <meta content="http://schemas.microsoft.com/intellisense/ie5" name="vs_targetSchema">
    </HEAD>
    <body MS_POSITIONING="GridLayout">
        <form id="Form1" method="post" runat="server">
            <script language="javascript">
                    function StringAdd(){
                    var username=document.getElementById("username").value;
                    var ret=ajaxstu.insertintdatabase.ServerSideAdd(username);
                    //window.alert(username);
                     alert(ret.value);
                      if(ret.value=="OK")
                     {
                         alert("保存成功!");
                           }
                        else
                       {
                         alert("失败!");
                           }
              }

            </script>
            <FONT face="宋体"><INPUT style="Z-INDEX: 101; LEFT: 472px; POSITION: absolute; TOP: 72px" onclick="javascript:StringAdd()"
                    type="button" value="Button"></FONT><INPUT id="username" style="Z-INDEX: 102; LEFT: 216px; POSITION: absolute; TOP: 56px" type="text"
                value="admin" name="username">
        </form>
    </body>
</HTML>





insertintdatabase.aspx.cs

private void Page_Load(object sender, System.EventArgs e)
        {

            // 在此处放置用户代码以初始化页面
            AjaxPro.Utility.RegisterTypeForAjax(typeof(ajaxstu.insertintdatabase));

        }
[AjaxPro.AjaxMethod]
        public string ServerSideAdd( string mytime)
        {
        try
            {
//                myconnection.Open();
                string ConnectionString=System.Configuration.ConfigurationSettings.AppSettings["ConnectionString"];
                SqlConnection Con = new SqlConnection(ConnectionString);
                Con.Open();
                //SqlCommand cd=new SqlCommand("insert into Users (PWD) values('fsadf')",Con);
                SqlCommand cd=new SqlCommand("insert into Users (PWD) values(@pwd)",Con);
                cd.Parameters.Add("@pwd",SqlDbType.VarChar,256);
                cd.Parameters["@PWD"].Value=mytime;
                cd.ExecuteNonQuery();
                return "OK";
            }
            catch
            {
                return "BAD";
            }
            finally
            {
//                myconnection.Close();
            }
            //return "OK";
        }




文章来自: 本站原创
引用通告: 查看所有引用 | 我要引用此文章
Tags: AJAX
相关日志:

评论: 1 | 引用: 0 | 查看次数: 7329
回复回复习惯2个人过[2006-09-20 05:45 PM | del]
挺好的,呵呵
发表评论
昵 称:
密 码: 游客发言不需要密码.
内 容:
验证码: 验证码
选 项:
虽然发表评论不用注册,但是为了保护您的发言权,建议您注册帐号.
字数限制 1000 字 | UBB代码 开启 | [img]标签 开启