CSharp Oracle 登陆
=======后台Oracle存储过程================
1、创建表
--判读表存在先删除
begin
EXECUTE IMMEDIATE 'DROP TABLE student';
EXCEPTION WHEN OTHERS THEN NULL;
end;
/
create table student(
stuId varchar(50) primary key not null, --Id
stuName varchar(50) not null, --用户名
stuPwd number(20) not null --密码(最后一个不能有逗号)
);
--插入数值
Insert into student values('001','张三',123);
Insert into student values('002','王五',123);
Insert into student values('003','李四',123);
Insert into student values('004','赵六',123);
commit; --提交
--测试创建表成功
select * from student;
2、创建登陆函数
create or replace function funLogin(name in varchar2,pwd in number)
return varchar2
is
co number;
begin
select count(*) into co from student
where stuName=name and stuPwd=pwd;
if co>1 then
return '用户'||name||',登陆成功!';
else
return '用户'||name||',登陆失败!';
end if;
end funLogin;
/
3、测试登陆函数
set serveroutput on; --显示输出信息
declare
name varchar2(50):='张三';
pwd number(20):=123;
re varchar2(50);
begin
select funLogin(name,pwd) into re from dual;
dbms_output.put_line(re);
end;
/
=======前台程序代码======================
1、在Web.config配置登陆信息
<appSettings >
<add key="linkOracle_01" value="Data Source=数据库;Persist Security Info=True;User ID=用户名;Password=密码;Unicode=True;" />
</appSettings>
2、前台Login.aspx
3、后台代码Login.aspx.cs
引用添加NET命名空间
代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
//引用-》添加命名空间
using System.Data.OracleClient;
using System.Data;
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(); ;
int pwd = Convert.ToInt32(txtPwd.Text.Trim());
string strConn = System.Configuration.ConfigurationManager.AppSettings["linkOracle_01"].ToString();
OracleConnection conn = new OracleConnection(strConn);
OracleCommand cmd = new OracleCommand("funLogin", conn);
cmd.CommandType = CommandType.StoredProcedure;
//参数
OracleParameter pName = new OracleParameter("name", OracleType.VarChar, 50);
pName.Value = name;
pName.Direction = ParameterDirection.Input;
OracleParameter pPwd = new OracleParameter("pwd", OracleType.Number, 20);
pPwd.Value = pwd;
pPwd.Direction = ParameterDirection.Input;
//返回值名称可以re随便写
OracleParameter pRe = new OracleParameter("re", OracleType.VarChar, 50);
pRe.Direction = ParameterDirection.ReturnValue;
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);
//或者这样注册 返回信息
//Page.RegisterStartupScript("re", "<script>alert('" + pRe + "')</script>");
conn.Close();
}
}
}