我有以下代码,现在我想在单击提交按钮时调用SQL的存储过程。
当点击提交按钮时,任何人都可以帮助我放置调用SP的代码吗?
Default.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>
Default.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>");
}
}
}
Details.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>
Details.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)
{
}
}
答案 0 :(得分: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;
答案 1 :(得分:0)
您可以使用基于存储过程的命令文本而不是使用普通命令文本; 首先使用out参数(以及参数)创建一个storedproedure:在参数中检查表的用户名和密码,Out参数是在检查数据库后返回用户是否是有效的。
所以,请使用
cmd.CommandText="StoredProcedureName";
cmd.CommandType=CommandType.StoredProcedure;
并使用out和in参数;获取out参数值后,您可以根据用户提供的凭据重定向到详细信息/错误页面。:)
答案 2 :(得分:0)
而不是您在下面的行中传递给命令对象的查询 - 传递存储过程名称。它应该在Button click事件中。你已经在正确的地方写了代码。
SqlCommand cmd = new SqlCommand("StoredProcedureName", con);
cmd.CommandType=CommandType.StoredProcedure;
cmd.ExecuteReader()
我希望这会有所帮助
答案 3 :(得分: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参数,如上一个答案所示...
答案 4 :(得分:0)
以下是您正在寻找的完整解决方案,
存储过程:
使用[YOURDB]
去
IF EXISTS(SELECT * FROM sysobjects WHERE type ='P'AND name ='sp_Login') 开始 DROP过程[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 output
) AS
BEGIN 设置NOCOUNT
DECLARE @ErrMsg NVARCHAR(510) - ,@ ErrorSeverity INT --16如果出现业务错误 BEGIN
SET @OutRes =(SELECT count(*)FROM Log_Users WHERE Username = @Username And [Password] = @UPassword)
END GOTO SUCCESS
SUCCESS: 返回1 CRASH: RAISERROR(@ ErrMsg,@ ErrorSeverity,1)与NOWAIT 结束 走 GRANT EXEC ON [dbo]。[sp_Login] TO PUBLIC
GO
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 ..