首先創建兩個TextBox 分別用來輸入用戶名和密碼
再創建個Button 用做登陸按鈕
cs文件中代碼如下:
using System;
using System.Data;
using System.Data.SqlClient; //別忘了加上這句
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
public partial class MasterPage : System.Web.UI.MasterPage
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
SqlConnection cnn = new SqlConnection();
SqlCommand cmm = new SqlCommand();
DataSet ds = new DataSet();
cnn.ConnectionString = "Data Source=localhost\\SQLEXPRESS;Initial Catalog=ks;Integrated Security=True";//其中ks為數據庫名
cnn.Open();
cmm.Connection = cnn;
cmm.CommandText ="select * from [ks].[dbo].[password] where name= '" + TextBox1.Text + " ' and password ='" + TextBox2.Text + "'"; //[password]為[表名]
SqlDataAdapter ada = new SqlDataAdapter(cmm);
ada.Fill(ds);
if (ds.Tables[0].Rows.Count > 0)
{
Response.write("登陸成功");
}
else
{
Response.Write("用戶名密碼錯誤");
}
}
protected void TextBox1_TextChanged(object sender, EventArgs e)
{
}
protected void TextBox2_TextChanged(object sender, EventArgs e)
{
}
aspx源代碼為:
<body>
<form id="form1" runat="server">
<div>
<table>
<tr>
<td style="width: 100px">
<asp:Label ID="Label1" runat="server" Text="用戶名"></asp:Label></td>
<td style="width: 100px">
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox></td>
</tr>
<tr>
<td style="width: 100px; height: 28px">
<asp:Label ID="Label2" runat="server" Text="密碼"></asp:Label></td>
<td style="width: 100px; height: 28px">
<asp:TextBox ID="TextBox2" runat="server"></asp:TextBox></td>
</tr>
<tr>
<td style="width: 100px">
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="登錄" /></td>
<td style="width: 100px">
<asp:Button ID="Button2" runat="server" Text="取消" /></td>
</tr>
</table>
</div>
</form>
</body>
最好能把錯誤詳細講壹下~