CSharp SQLServer 登陆
=======后台SQLServer存储过程================
--创建数据库
create database Stu;
--创建表
use MyShool;
if exists(select * from sys.tables where name='student')
drop table student;
go
create table Student(
stuId int identity(1,1) primary key,
stuName varchar(50) null,
stuPwd varchar(50) null,
stuAge int null,
);
--插入数值
Insert into student(stuName,stuPwd,stuAge) values('张三',123,10);
Insert into student(stuName,stuPwd,stuAge) values('王五',123,20);
Insert into student(stuName,stuPwd,stuAge) values('李四',123,30);
Insert into student(stuName,stuPwd,stuAge) values('赵六',123,40);
commit;
--查询数据
select * from student;
--创建存储过程
create procedure [dbo].[proLogin](
@i_name varchar(50),
@i_pwd varchar(50),
@o_re varchar(50) output) --返回信息
as
declare @co int; --查到总行数变量
begin
-- set @co=( select count(*) from student
-- where stuName=@i_name and stuPwd=@i_pwd);
select @co=count(*) from student
where stuName=@i_name and stuPwd=@i_pwd;
if @co = 1
begin
set @o_re='用户'+@i_name+'登陆成功!';
return 11;
end;
else
begin
set @o_re='用户'+@i_name+'登陆失败!';
return 22;
end;
end;
--测试存储过程
declare
@return_value int,
@o_re varchar(50)
exec @return_value=proLogin
@i_name = N'张三',
@i_pwd = N'123',
@o_re = @o_re OUTPUT
select @o_re,@return_value
=======前台程序代码======================
1、在Web.config配置登陆信息
<connectionStrings>
<add name="字符串名称" connectionString="server=服务器地址;database=数据库名称;uid=登陆名;pwd=密码;"/>
</connectionStrings>
2、前台Login.aspx
3、后台代码Login.aspx.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
namespace WebApplication1
{
public partial class Login : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void btnLogin_Click(object sender, EventArgs e)
{
//用户名和密码可以从外部输入
string name = txtName.Text.Trim(); ;
string pwd = txtPwd.Text.Trim();
string strConn = System.Configuration.ConfigurationManager.ConnectionStrings["LinkSqlServer1"].ToString();
SqlConnection conn = new SqlConnection(strConn);
conn.Close();
SqlCommand cmd = new SqlCommand("proLogin", conn);
cmd.CommandType = CommandType.StoredProcedure;
//参数
SqlParameter pName = new SqlParameter("@i_name", SqlDbType.VarChar, 50);
pName.Value = name;
pName.Direction = ParameterDirection.Input;
SqlParameter pPwd = new SqlParameter("@i_pwd", SqlDbType.VarChar, 50);
pPwd.Value = pwd;
pPwd.Direction = ParameterDirection.Input;
//输出
SqlParameter pRe = new SqlParameter("@o_re", SqlDbType.VarChar, 50);
pRe.Direction = ParameterDirection.Output;
cmd.Parameters.Add(pName);
cmd.Parameters.Add(pPwd);
cmd.Parameters.Add(pRe);
conn.Open();
cmd.ExecuteNonQuery();
ScriptManager.RegisterStartupScript(this.btnLogin, btnLogin.GetType(),
"re", "alert('" + pRe.Value.ToString() + "');", true);
conn.Close();
}
}
}