Login.aspx
在頁面上建用戶名和密碼的文本框,登陸按鈕
-----------------------
Login.aspx.cs
protected void Submit_Click(object sender, EventArgs e)
{
DataAccess.Class1 dac = new DataAccess.Class1();
if (dac.CheckLogin(this.UName.Text, this.pwd.Text))
//Response.Write("Success!");
//Response.Redirect("Default.aspx?");
Server.Transfer("Default.aspx");
else
Response.Write("Failed!");
//Server.Transfer("Login.aspx");
}
----------------------------------------------------
Default.aspx
在頁面上建GridView,要寫內容到數據庫的文本框和按鈕
-------------------------
Default.aspx.cs
protected void Page_Load(object sender, EventArgs e)
{
if(!IsPostBack)
{
BindData();
}
}
public void BindData()
{//51aspx.com
DataAccess.Class1 dac = new DataAccess.Class1();
DataSet ds = dac.getUsers();
this.GridView1.DataSource = ds.Tables[0].DefaultView;
this.GridView1.DataBind();
}
protected void Button1_Click(object sender, EventArgs e)
{
DataAccess.Class1 dac = new DataAccess.Class1();
DataSet ds = dac.getUsers();
DataRow dr = ds.Tables[0].NewRow();
dr["UsersName"] = this.TextBox1.Text;
dr["Passwords"] = this.TextBox2.Text;
dr["BlogName"] = this.TextBox3.Text;
ds.Tables[0].Rows.Add(dr);
dac.updataBystoredprocedure(ds);
BindData();//51aspx.com
}
-------------------------------------------------------------
Common類庫:
//cn
public const string ConnectString = "server=localhost;database=DBxBlog;UID=xBlog;PassWord=123456";
//Sql
public const string Sql_getUser = "select * from Users";
//Table
public const string table_Users = "Users";
//StoredProcedure
public const string StoredProcedure_insert = "insertUsers";
-------------------------------------------------------------------
DataAccess類庫(數據層):
SqlConnection cn = new SqlConnection(Common.Class1.ConnectString); SqlDataAdapter da; DataSet ds=new DataSet(); //gettable public DataSet getUsers() { SqlCommand cmd = new SqlCommand(Common.Class1.Sql_getUser,cn); da = new SqlDataAdapter(cmd); da.Fill(ds,Common.Class1.table_Users); return ds; } //Login public bool CheckLogin(string name, string pwd) { try { SqlCommand cmd = new SqlCommand("select count(*) from Users where UsersName='" + name + "'and Passwords='" + pwd + "'", cn); cn.Open(); int count = -1; count = Convert.ToInt32(cmd.ExecuteScalar()); if (count < 1) return false; return true; } catch (Exception ex) { return false; } } //textbox updata public void updata(DataSet ds) { SqlCommand cmd = new SqlCommand("insert into Users(UsersName,Passwords,BlogName)valuse(@UsersName,@Passwords,@BlogName)", cn); cmd.Parameters.Add("@UsersName",SqlDbType.VarChar,20,"UsersName"); cmd.Parameters.Add("@Passwords", SqlDbType.VarChar, 50, "Passwords"); cmd.Parameters.Add("@BlogName", SqlDbType.VarChar, 20, "BlogName"); da=new SqlDataAdapter (); da.InsertCommand=cmd; cn.Open(); da.Update(ds,Common.Class1.table_Users); cn.Close(); } //stroedprocedure updata public void updataBystoredprocedure(DataSet ds) { SqlCommand cmd = new SqlCommand(Common.Class1.StoredProcedure_insert,cn); cmd.Parameters.Add("@UsersName",SqlDbType.VarChar,20,"UsersName"); cmd.Parameters.Add("@Passwords", SqlDbType.VarChar, 50, "Passwords"); cmd.Parameters.Add("@BlogName", SqlDbType.VarChar, 20, "BlogName"); cmd.CommandType = CommandType.StoredProcedure; da=new SqlDataAdapter(); da.InsertCommand=cmd; cn.Open(); da.Update(ds,Common.Class1.table_Users); cn.Close(); }