MSSQL储蓄过程学习手记

存储过程分为两类:

系统提供的存储过程(sp_为前缀并且系统存储过程主要是从系统表中获取信息,从而为系统管理员管理SQL Server);

用户自定义存储过程(由用户创建,并能完成某一特定功能);

存储过程优势:

1.存储过程在被创建以后,可以在程序中被多次调用,而不必重新编写该存储过程的SQL语句,而且数据库专业人员可随时对存储过程进行修改,但对应用程序源代码毫无影响。因为应用程序源代码只包含存储过程的调用语句,从而极大地提高了程序的可移植性。

2.存储过程能够实现快速的执行速度

如果某一操作包含大量的Transaction-SQL 代码,,或分别被多次执行,那么存储过程要比批处理的执行速度快很多,因为存储过程是预编译的,在首次运行一个存储过程时,查询优化器对其进行分析优化,并给出最终被存在系统表中的执行计划,而批处理的Transaction-SQL 语句在每次运行时都要进行编译和优化,因此速度相对要慢一些。

3.存储过程能够减少网络流量

对于同一个针对数据数据库对象的操作,如查询修改,如果这一操作所涉及到的Transaction-SQL 语句被组织成一存储过程,那么当在客户计算机上调用该存储过程时,网络中传送的只是该调用语句,否则将是多条SQL 语句从而大大增加了网络流量降低网络负载。

4.存储过程可被作为一种安全机制来充分利用

系统管理员通过,对执行某一存储过程的权限进行限制,从而能够实现对相应的数据访问权限的限制。

储蓄过程格式:

Sql存储过程基本语法之存储过程格式

 创建存储过程

Create Proc dbo.存储过程名

存储过程参数

AS

执行语句

RETURN

执行存储过程

GO

测试:

-- 要创建存储过程的数据库
Use db_NetStore
-- 判断要创建的存储过程名是否存在
if Exists(Select name From sysobjects Where name='proc_UserLogin' And  type='P')
-- 删除存储过程
Drop Procedure dbo.proc_UserLogin
Go
-- 创建存储过程
Create Proc dbo.proc_UserLogin
@UserName varchar(16),
@Password varchar(50)
AS
-- 存储过程语句体
select * from tb_Member 
where UserName=@UserName and Password=@Password
RETURN
-- 执行
GO
-- 执行存储过程
exec proc_UserLogin 'lidy','111111'

创建好后用管理工具可以在 <可编程性>下面的<储蓄过程>可以看到储蓄的存储过程。

后台程序测试:

<%@ WebHandler Language="C#" Class="login" %>
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Web;

 public class login : IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {
            //请求参数
            string username = context.Request["username"];
            string password = context.Request["password"];
            if (username != null && password != null)
            {
                //连接字符串
                string sql = @"Data Source=****;Initial Catalog=db_netstore;User ID=sa;Password=mm111111;";
                //命令参数名称
                SqlParameter[] prms = new SqlParameter[]{           
                 new SqlParameter("@username", SqlDbType.VarChar,50),
                 new SqlParameter("@password",SqlDbType.VarChar,50)
            };
                //赋值
                prms[0].Value = username;
                prms[1].Value = password;
                //读取一条数据
                if (SqlHelper.ExecuteReader(sql, "proc_UserLogin", prms).Read())
                {
                    context.Response.Write("success");//成功返回
                }
                else
                {
                    context.Response.Write("fail");
                }
            }
            else
            {
                context.Response.Write("请求错误");
            } 
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }

前台代码(注册事件以后补):

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <title></title>
    <script src="js/jquery.js"></script>
    <script type="text/javascript">
        function login() { //登录
            var name = $("#username").val();
            var password = $("#password").val();
            if (name != "" && password != "") {
                //发送AJAX请求
                $.post("login.ashx", { username: name, password: password },
                    function (data) { //回传函数
                        if (data == "success") {
                            $("#ullogin").html("<li><span>" + $("#username").val() + "您好,欢迎登录</span></li>");
                        }
                        else {
                            $("#msg").html("用户名密码错误");
                        }
                    });
            }
            else {
                $("#msg").html("请输入用户名和密码");
            }
        }
        function reg() { //注册
            var input = $("#reg input");
               
        }

        function display() { //显示隐藏注册ul
            var ul = $("#reg ul").css("display");
            if (ul == "none") {
                $("#reg ul").css("display","block");
            }
            else {
                $("#reg ul").css("display", "none");
            }
        }
    </script>
    <style type="text/css">
        ul {
            list-style-type: none;
        }
        #msg {
            display: inline-block;
            width: 140px;
            height: 16px;
        }
        #reg {
            text-align: center;
        }
            #reg ul {
                margin: 0px auto;
                display:none;
            }
                #reg ul li {
                    margin-bottom: 10px;
                }
                    #reg ul li span {
                        display:inline-block;
                        width:80px;
                        margin-right: 15px;
                    }
    </style>
</head>
<body>
        <ul id="ullogin">
            <li><span>用户名:</span> <input type="text" id="username"/></li>
            <li><span>密  码:</span> <input type="password" id="password"/></li>
            <li><span id="msg"></span>
                <input type="button" value="登录" onclick="login()"/>
                <input type="button" value="注册" onclick="display()"/>
            </li>
        </ul> 
    <div id="reg">
        <ul>
            <li><h2>注册新用户</h2></li>
            <li><span>用户名</span><input type="text" /></li>
            <li><span>密码</span><input type="password" /></li>
            <li><span>确认密码</span><input type="password" /></li>
            <li><span>姓名</span><input type="text" /></li>
            <li><span>性别</span><input type="text" /></li>
            <li><span>联系电话</span><input type="text" /></li>
            <li><input type="button" value="注册" onclick="reg()"/><input type="button" value="取消" onclick="display()"/></li>
        </ul>
    </div>     
</body>
</html>

OK~

 

 

 

posted @ 2012-10-23 22:33  安之若素冷暖自知  阅读(261)  评论(0编辑  收藏  举报