データベース:アプリ、テーブル:学生
db.csファイル
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.SqlClient;
///
/// dbの概要説明
///
public class db
{ public db(){ / / // TODO:ここで追加コンストラクタロジック// }パブリック静的SqlConnectionオブジェクトのCreateConnection(){ SqlConnectionのSqlConnectionの新しい新しい= CON(「データソースPC = 201 510 151 352 \ MSSQLSERVER1; =アプリ初期カタログ;ユーザーID = SA; Pwd = 1234 "); return con;
}
}
Login.aspxファイル
<%@ Page Language =“ C#” AutoEventWireup =“ true” CodeFile =“ Login.aspx.cs” Inherits = "_ Default"%>
ユーザーログインページ
ユーザー名:
パスワード:
Login.aspx.csファイル
システムを使用する;
System.Collections.Genericを使用する;
System.Linqを使用する;
System.Webを使用する;
System.Web.UIを使用する;
System.Web.UI.WebControlsを使用する;
System.Data.SqlClientを使用します。
System.Dataを使用する;
public partial class _Default:System.Web.UI.Page
{ protected void Page_Load(object sender、EventArgs e){
}
protected void btnLogin_Click(object sender, EventArgs e)
{
if ((tbusername.Text == "") || (tbpsw.Text == ""))
{
Response.Write(@"<script>alert('用户名与密码不能为空!');</script>");
}
else
{
SqlConnection con = db.CreateConnection();
con.Open();
string strSql = "select password from Student where username='" + tbusername.Text + "'";
SqlCommand cmd = new SqlCommand(strSql, con);
DataSet ds = new DataSet();
SqlDataAdapter da = new SqlDataAdapter(strSql, con);
da.Fill(ds, "Student");
try
{
if (tbpsw.Text == ds.Tables[0].Rows[0].ItemArray[0].ToString().Trim())
{
string curuser = tbusername.Text;
Response.Write(@"<script>alert('登录成功,欢迎你!');</script>");
}
else
{
Response.Write(@"<script>alert('用户名或者密码错误!');</script>");
}
}
catch
{
Response.Write(@"<script>alert('Sorry!你输入的用户名不存在!');</script>");
}
con.Close();
}
/
/*
if (tbusername.Text == "")
{
Response.Write(@"<script>alert('用户名不能为空!');</script>");
goto abc;
}
if (tbpsw.Text == "")
{
Response.Write(@"<script>alert('密码不能为空!');</script>");
goto abc;
}
string username = tbusername.Text;
string password = tbpsw.Text;
String connstr = "Data Source=PC-201510151352\\MSSQLSERVER1;Initial Catalog=app;User Id=sa;Pwd=1234";
string sql = "select * from Student where username=@username and password=@password";
SqlParameter[] parameters = { new SqlParameter("@username", username), new SqlParameter("@password", password) };
using (SqlConnection conn = new SqlConnection(connstr))
{
conn.Open();
using (SqlCommand cmd = conn.CreateCommand())
{
cmd.CommandText = sql;
cmd.Parameters.AddRange(parameters);
DataSet ds = new DataSet();
SqlDataAdapter adapter = new SqlDataAdapter(cmd);
adapter.Fill(ds);
DataTable table = ds.Tables[0];
if (table == null)
Response.Write(@"<script>alert('登录成功!');</script>");
else
Response.Write(@"<script>alert('登录失败!');</script>");
}
}
abc: ;
*/
///
}
protected void btnRegister_Click(object sender, EventArgs e)
{
if((tbusername.Text=="")||(tbpsw.Text==""))
{
Response.Write(@"<script>alert('用户名与密码不能为空!');</script>");
}
else
{
try
{
SqlConnection con = db.CreateConnection();
con.Open();
string strsql = "insert into Student values('" + tbusername.Text + "','" + tbpsw.Text + "')";
SqlCommand cmd=new SqlCommand(strsql,con);
cmd.ExecuteNonQuery();
con.Close();
tbusername.Text="";
tbpsw.Text="";
Response.Write(@"<script>alert('注册成功!欢迎登录!');</script>");
}
catch
{
Response.Write(@"<script>alert('用户名已存在!');</script>");
}
}
}
/*
String connstr = "Data Source=PC-201510151352\\MSSQLSERVER1;Initial Catalog=app;User Id=sa;Pwd=1234";
SqlConnection con = new SqlConnection();
con.ConnectionString = connstr;
con.Open();
SqlCommand cmd = new SqlCommand();
cmd.Connection = con;
String sql = String.Format("Insert into Student values('" + tbusername.Text + "','" + tbpsw.Text + "')");
cmd.CommandText = sql;
cmd.ExecuteNonQuery();
Response.Write(@"<script>alert('注册成功!');</script>");
// Response.Write(@"<script>alert('注册成功!');window.location='Register.aspx';</script>");
con.Close();
*/
///
}