0

我有以下代码,现在我想在单击提交按钮时调用 SQL 的存储过程。

任何人都可以帮助我在单击提交按钮时将调用 SP 的代码放在哪里?

默认.aspx

  <%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!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 id="Head1" runat="server">
<title>Login Form</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<table align="center">

<strong>Login Form</strong>

<tr>
<td>
Username:
</td>
<td>
<asp:TextBox ID="txtUserName" runat="server"/>
<asp:RequiredFieldValidator ID="rfvUser" ErrorMessage="Please enter Username" ControlToValidate="txtUserName" runat="server" />
</td>
</tr>
<tr>
<td>
Password:
</td>
<td>
<asp:TextBox ID="txtPWD" runat="server" TextMode="Password"/>
<asp:RequiredFieldValidator ID="rfvPWD" runat="server" ControlToValidate="txtPWD" ErrorMessage="Please enter Password"/>
</td>
</tr>
<tr>
<td>
</td>
<td>
<asp:Button ID="btnSubmit" runat="server" Text="Submit" onclick="btnSubmit_Click" />
</td>
</tr>
</table>
</div>
</form>
</body>
</html>

默认.aspx.cs

using System;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data;


public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["dbconnection"].ConnectionString);
        con.Open();
        SqlCommand cmd = new SqlCommand("select * from Log_Users where UserName =@username and Password=@password", con);
        cmd.Parameters.AddWithValue("@username", txtUserName.Text);
        cmd.Parameters.AddWithValue("@password", txtPWD.Text);
        SqlDataAdapter da = new SqlDataAdapter(cmd);
        DataTable dt = new DataTable();
        da.Fill(dt);
        if (dt.Rows.Count > 0)
        {
            Response.Redirect("Details.aspx");
        }
        else
        {
            ClientScript.RegisterStartupScript(Page.GetType(), "validation", "<script language='javascript'>alert('Invalid Username Or Password')</script>");
        }
    }
}

详细信息.aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Details.aspx.cs" Inherits="Details" %>

<!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 id="Head1" runat="server">
    <title>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <table align="center">
        <tr>
    <td>
      <asp:Label ID="lblDisplay" runat="server" Text="Welcome to our sample Site" Font-Bold="true"></asp:Label>
    </td>
    </tr>
    </table>

    </div>
    </form>
</body>
</html>

详细信息.aspx.cs

using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

public partial class Details : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
}
4

5 回答 5

1

使用您的 select 语句编写存储过程:

select * from Log_Users where UserName =@username and Password=@password

并更改您的 ADO.net 代码以使用此存储过程,

 SqlCommand cmd = new SqlCommand("your sp name", con);

 cmd.CommandTpe = CommandType.StoredProcedure;
于 2013-07-16T06:26:56.817 回答
0

您可以使用基于存储过程的命令文本,而不是使用普通的命令文本;首先创建一个带有 out 参数(以及 in 参数)的存储过程: In 参数是根据表检查用户名和密码,Out 参数是在检查数据库后返回用户是否有效。

所以,利用

cmd.CommandText="StoredProcedureName";
cmd.CommandType=CommandType.StoredProcedure;

并使用 out 和 in 参数;并且得到out参数值后,可以根据用户提供的凭据重定向到详细信息/错误页面.. :)

于 2013-07-16T06:26:24.390 回答
0

而不是您传递给下一行中的命令对象的查询 - 传递存储过程名称。它应该在按钮单击事件中。您已经在正确的位置编写了代码。

    SqlCommand cmd = new SqlCommand("StoredProcedureName", con);

   cmd.CommandType=CommandType.StoredProcedure;

  cmd.ExecuteReader()

我希望这有帮助

于 2013-07-16T06:31:12.030 回答
0

正如您所要求的存储过程..下面是带有 OUTPUT 参数的那个..

IF EXISTS (SELECT * FROM sysobjects WHERE type = 'P' AND name = 'proc_name')
BEGIN
DROP Procedure [dbo].[proc_name]
END

SET ANSI_NULLS ON
SET QUOTED_IDENTIFIER ON
GO


/*
File Name : proc_name.sql
Description : 
Author : Vinay.T
Date :
*/

CREATE Procedure [dbo].[proc_name]
(
     @Username VarChar(50)
    ,@UPassword varChar(50)
    ,@OutRes int OUTPUT 
)
AS

BEGIN
SET NOCOUNT ON

DECLARE @ErrMsg NVARCHAR(510) --
        ,@ErrorSeverity INT --16 if business error
BEGIN

SET @OutRes = (SELECT count(*) FROM [dbo].Log_Users WHERE Username = @Username And [Password] = @UPassword)


END
GOTO SUCCESS

SUCCESS:
RETURN 1
CRASH:
RAISERROR (@ErrMsg, @ErrorSeverity, 1) WITH NOWAIT
END
GO
GRANT EXEC ON [dbo].[proc_name] TO PUBLIC

GO

============================================

执行上面的 sp 并使用 DataReader 来获取 Output 参数,如上一个答案中所建议的那样......

于 2013-07-16T07:29:13.473 回答
0

以下是您正在寻找的完整解决方案,

  1. 存储过程:

    使用 [YOURDB]

    IF EXISTS (SELECT * FROM sysobjects WHERE type = 'P' AND name = 'sp_Login') BEGIN DROP Procedure [dbo].[sp_Login] END

    SET ANSI_NULLS ON SET QUOTED_IDENTIFIER ON GO

    /* 文件名:sp_Login.sql 描述:作者:Vinay.T 日期:*/

    创建过程 [dbo].[sp_Login] (@Username VarChar(50),@UPassword varChar(50),@OutRes int 输出

    ) 作为

    BEGIN SET NOCOUNT ON

    DECLARE @ErrMsg NVARCHAR(510) -- ,@ErrorSeverity INT --16 如果业务错误 BEGIN

    SET @OutRes = (SELECT count(*) FROM Log_Users WHERE Username = @Username And [Password] = @UPassword)

    结束转到成功

    成功:返回 1 崩溃:RAISERROR (@ErrMsg, @ErrorSeverity, 1) WITH NOWAIT END GO GRANT EXEC ON [dbo].[sp_Login] TO PUBLIC

2:按钮点击事件代码:

SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["dbconnection"].ConnectionString);
        con.Open();
        SqlCommand cmd = new SqlCommand("sp_Login", con);
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.Parameters.AddWithValue("@Username", txtUserName.Text);
        cmd.Parameters.AddWithValue("@UPassword", txtPWD.Text);
        cmd.Parameters.Add("@OutRes", SqlDbType.Int).Direction = ParameterDirection.Output;
        cmd.ExecuteNonQuery();

        int output = (int)cmd.Parameters["@OutRes"].Value;
        if (output == 1)
        {
            Response.Redirect("Details.aspx");
        }
        else
        {
            ClientScript.RegisterStartupScript(Page.GetType(), "validation", "<script language='javascript'>alert('Invalid Username Or Password')</script>");
        }

让我知道这不起作用..?对您的数据库运行 sp ..

于 2013-07-16T09:13:14.500 回答