存储过程

来源:互联网 发布:用matlab解决优化问题 编辑:程序博客网 时间:2024/06/10 00:33

1.前台

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm2.aspx.cs" Inherits="存储过程.WebForm2" %>

<!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">
    <div>
    <asp:Button ID="Button1" runat="server" Text="插入" onclick="Button1_Click" />
    </div>
    </form>
</body>
</html>

后台

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;
using System.Data;

namespace 存储过程
{
    public partial class WebForm2 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            string strcon = @"Data Source=PC-20121114LMOU;Initial Catalog=News;Persist Security Info=True;User ID=sa;Password=842674";
            SqlConnection conn = new SqlConnection(strcon);
            conn.Open();
            SqlCommand cmd = new SqlCommand();
            cmd.Connection = conn;
            cmd.CommandText = "Pro_GetMaxId";
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.Add("@maxid",SqlDbType.Int);
            //表明此@maxid为输出参数
            cmd.Parameters["@maxid"].Direction = ParameterDirection.Output;
            cmd.ExecuteNonQuery();
            Response.Write(cmd.Parameters["@maxid"].Value.ToString());
            cmd.Dispose();
            conn.Dispose();
        }
    }
}

 

 

 

原创粉丝点击