using System; using System.Collections; using System.ComponentModel; using System.Data; using System.Drawing; using System.Web; using System.Web.SessionState; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.HtmlControls; using System.Data.SqlClient; namespace 第n次 { /// <summary> /// WebForm1 的摘要说明。 /// </summary> public class WebForm1 : System.Web.UI.Page { protected System.Web.UI.WebControls.Label Label1; protected System.Web.UI.WebControls.Button Button1; protected System.Web.UI.WebControls.Button Button2; protected System.Web.UI.WebControls.Label Label2; protected System.Data.DataSet dataSet1; protected System.Web.UI.WebControls.Label Message; protected System.Web.UI.WebControls.TextBox nameTextBox; protected System.Web.UI.WebControls.TextBox PasswordTextBox; protected System.Web.UI.WebControls.Label Label3;
private void Page_Load(object sender, System.EventArgs e) { // 在此处放置用户代码以初始化页面 } #region Web 窗体设计器生成的代码 override protected void OnInit(EventArgs e) { // // CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。 // InitializeComponent(); base.OnInit(e); }
/// <summary> /// 设计器支持所需的方法 - 不要使用代码编辑器修改 /// 此方法的内容。 /// </summary> private void InitializeComponent() { this.dataSet1 = new System.Data.DataSet(); ((System.ComponentModel.ISupportInitialize)(this.dataSet1)).BeginInit(); this.Button1.Click += new System.EventHandler(this.Button1_Click); this.Button2.Click += new System.EventHandler(this.Button2_Click); // // dataSet1 // this.dataSet1.DataSetName = "NewDataSet"; this.dataSet1.Locale = new System.Globalization.CultureInfo("zh-CN"); this.Load += new System.EventHandler(this.Page_Load); ((System.ComponentModel.ISupportInitialize)(this.dataSet1)).EndInit(); } #endregion private void Button2_Click(object sender, System.EventArgs e) { Response.Redirect ("WebForm2.aspx?"); } private void Button1_Click(object sender, System.EventArgs e) { string s_name= nameTextBox.Text .Trim (); string s_Password =PasswordTextBox.Text .Trim (); string strsel = "select name,Password from userdata where name = '"+s_name+"' and Password = '"+s_Password+"'"; //string strsel = "select User_Name,User_Password from Info_userdata where name = '"+s_name+"'";
using System; using System.Data; using System.Configuration; 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; using System.Web.UI.Page.User; public partial class _Default : System.Web.UI.Page {
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; using System.Data.SqlClient;namespace WindowsApplication11 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { SqlConnection my = new SqlConnection(); my.ConnectionString = "server=localhost;uid=sa;database=pubs"; my.Open(); string se = "select * from userinfo where uid='" + this.textBox1.Text + "'and pwd='" + this.textBox2.Text + "'"; SqlCommand cmd = new SqlCommand(se, my); SqlDataReader reader = cmd.ExecuteReader(); reader.Read(); try { if (textBox1.Text.Equals(reader.GetString(0))) { Form2 f1 = new Form2(); this.Hide(); f1.ShowDialog(); this.Close(); } } catch { MessageBox.Show("登陆失败!!!");//异常提示 } finally { my.Close(); cmd.Dispose(); my.Dispose(); } } } }
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
namespace 第n次
{
/// <summary>
/// WebForm1 的摘要说明。
/// </summary>
public class WebForm1 : System.Web.UI.Page
{
protected System.Web.UI.WebControls.Label Label1;
protected System.Web.UI.WebControls.Button Button1;
protected System.Web.UI.WebControls.Button Button2;
protected System.Web.UI.WebControls.Label Label2;
protected System.Data.DataSet dataSet1;
protected System.Web.UI.WebControls.Label Message;
protected System.Web.UI.WebControls.TextBox nameTextBox;
protected System.Web.UI.WebControls.TextBox PasswordTextBox;
protected System.Web.UI.WebControls.Label Label3;
private void Page_Load(object sender, System.EventArgs e)
{
// 在此处放置用户代码以初始化页面
} #region Web 窗体设计器生成的代码
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.dataSet1 = new System.Data.DataSet();
((System.ComponentModel.ISupportInitialize)(this.dataSet1)).BeginInit();
this.Button1.Click += new System.EventHandler(this.Button1_Click);
this.Button2.Click += new System.EventHandler(this.Button2_Click);
//
// dataSet1
//
this.dataSet1.DataSetName = "NewDataSet";
this.dataSet1.Locale = new System.Globalization.CultureInfo("zh-CN");
this.Load += new System.EventHandler(this.Page_Load);
((System.ComponentModel.ISupportInitialize)(this.dataSet1)).EndInit(); }
#endregion private void Button2_Click(object sender, System.EventArgs e) {
Response.Redirect ("WebForm2.aspx?");
} private void Button1_Click(object sender, System.EventArgs e)
{
string s_name= nameTextBox.Text .Trim ();
string s_Password =PasswordTextBox.Text .Trim ();
string strsel = "select name,Password from userdata where name = '"+s_name+"' and Password = '"+s_Password+"'";
//string strsel = "select User_Name,User_Password from Info_userdata where name = '"+s_name+"'";
SqlConnection cn = new SqlConnection("server=ED32E0D74620400;uid=sa;pwd=;database=kang");
SqlCommand cmd = new SqlCommand(strsel, cn);
cn.Open();
SqlDataReader myDr = cmd.ExecuteReader();
if(myDr.Read())
{
this.Visible=false;
Response.Redirect ("WebForm3.aspx?");
}
else
{
Response.Write("密码和用户不一致");
}
myDr.Close();
cn.Close();
}
}
}
using System.Data;
using System.Configuration;
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;
using System.Web.UI.Page.User; public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{ }
protected void SqlDataSource1_Selecting(object sender, SqlDataSourceSelectingEventArgs e)
{ }
protected void rb1_SelectedIndexChanged(object sender, EventArgs e)
{ } protected void Button1_Click(object sender, System.EventArgs e)
{
if (Page.IsValid == true)
{
User user = new User();
string userid = "";
SqlDataReader recs =
user.GetUserButton(username.Text.Trim(),
UserDB.Encrypt(Password.text.Trim()));
if (recs.Read())
{
userid = recs["User_id"].ToString();
}
recs.Close();
if ((userid != null) && (userid != ""))
{
Session["UserID"]=userid;
Response.Redirect("~/DesktopModules/index.aspx");
}
else
{
Mseeage.Text=" 你输入的用户名称或者密码有误,请重新输入!";
}
} }
pubic SqlDataReader GetUserButton(String sUsername,String sPassword)
{
Database db = new Database();
SqlDataReader dr=null;
SqlParameter[]param={
db.CreateInParam("@username",SqlDbType.VarChar,50,sUsername),
db.CreateInParam("@Password",SqlDbType.VarChar,255.sPassword)
};
db.RunProc("Pr_GetUserButton",param,out dr);
return dr;
}
}
}}
这是我的程序,还没有加验证码那一块,结果出现了以下两个错误:
错误 1 应输入 ; D:\网站1\index.aspx.cs 56 25 D:\网站1\
错误 2 应输入类型、命名空间定义或文件尾 D:\网站1\index.aspx.cs 68 5 D:\网站1\
错误 1 应输入 ; D:\网站1\index.aspx.cs 56 25 D:\网站1\
这是运行后的错误!不知道是什么原因阿!
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false); FrmLogin winLogin = new FrmLogin();
winLogin.ShowDialog();
if (ClsApp.blnLogin) //ClsApp作全局对象用,包含静态Public变量和函数
{
Application.Run(new FrmMain());
} }//end main--------------------------------------------------------------
下面是Login窗口的登录代码: //确定按钮处理事件
private void btnOk_Click()
{
if (funLogin())
{
... //其它处理代码
this.Close();
}
}
System.Data.SqlClient.SqlConnection cnSql; //用户登录验证处理函数
private bool funLogin()
{
if (txtUser.Text.Trim() == "")
{
MessageBox.Show("用户ID不能为空!", "注意", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
return false;
} labMessage.Text = "正在登录,请稍候...";
Application.DoEvents(); //下面是数据库连接函数(该会的吧)
if (ClsApp.funSqlConnect(strServerName, strDatabase, strUser, strPass, cnSql)< 0)
{
labMessage.Text = "连接失败!";
MessageBox.Show("Sorry!由于网络原因数据库联接失败!\r\n" + ClsApp.strErrMessage, "注意", MessageBoxButtons.OK, MessageBoxIcon.Error);
return false;
} string strPass, strTemp;
System.Data.SqlClient.SqlCommand cmSql = new System.Data.SqlClient.SqlCommand();
System.Data.SqlClient.SqlDataReader drSql; cmSql.Connection = cnSql;
cmSql.CommandText = "SELECT * FROM user WHERE (userid = '" + txtUser.Text.Trim() + "')";
try
{
if (cnSql.State == ConnectionState.Closed) ClsApp.cnSql.Open();
drSql = cmSql.ExecuteReader();
if (drSql.Read())
{
strPass = drSql["password"].ToString();
//strTemp = ClsApp.funGetMD5Code(txtPass.Text.Trim(), txtUser.Text.Trim().ToUpper());//你暂时可以不用加密
strTemp = txtPass.Text.Trim();
if (strTemp == strPass)
{
//ClsApp.strUserID和ClsApp.strUserName都是静态变量可供后面程序使用
ClsApp.blnLogin = true;
ClsApp.strUserID = txtUser.Text.Trim();
ClsApp.strUserName = drSql["named"].ToString();
labMessage.Text = "登录成功.";
drSql.Close();
}
else
{
drSql.Close();
ClsApp.blnLogin = false;
labMessage.Text = "登录失败!";
txtPass.Select(0, txtPass.Text.Length);
MessageBox.Show("密码验证错误!", "注意", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
return false;
}
}
else
{
drSql.Close();
ClsApp.blnLogin = false;
labMessage.Text = "登录失败!";
txtUser.Select(0, txtUser.Text.Length);
MessageBox.Show("用户验证错误!", "注意", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
return false;
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "注意", MessageBoxButtons.OK, MessageBoxIcon.Error);
return false;
}
finally
{
cnSql.Close();
}
return true; }//end function
-------------------------------------------------------------------------------------
下面是ClsApp类里数据库连接函数(做成函数方便你以后调用): public static int funSqlConnect(string asServerName, string asDataBase, string asUserId, string asPassword, System.Data.SqlClient.SqlConnection acnSql)
{
//说明:连接SQL数据库
//参数:asServerName(服务器名)、asDataBase(数据库名)、asUserId(用户ID)、asPassword(口令)、acnSql(保存SQL连接对象)
//返回:0(成功)
string lsCn; lsCn = "data source=" + asServerName + ";initial catalog=" + asDataBase + ";user id=" + asUserId + ";password=" + asPassword;
try
{
if (acnSql == null)
{
acnSql = new System.Data.SqlClient.SqlConnection(lsCn);
}
else
{
acnSql.ConnectionString = lsCn;
}
acnSql.Open();
}
catch (Exception ex)
{
intActionCode = -1; //可省略
strErrMessage = ex.Message; //可省略
//MessageBox.Show(Err.Description, "注意", MessageBoxButtons.OK, MessageBoxIcon.Error)
return intActionCode;
}
return 0;
}
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;namespace WindowsApplication11
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
} private void button1_Click(object sender, EventArgs e)
{
SqlConnection my = new SqlConnection();
my.ConnectionString = "server=localhost;uid=sa;database=pubs";
my.Open();
string se = "select * from userinfo where uid='" + this.textBox1.Text + "'and pwd='" + this.textBox2.Text + "'";
SqlCommand cmd = new SqlCommand(se, my);
SqlDataReader reader = cmd.ExecuteReader();
reader.Read(); try
{
if (textBox1.Text.Equals(reader.GetString(0)))
{
Form2 f1 = new Form2();
this.Hide();
f1.ShowDialog();
this.Close(); }
} catch
{
MessageBox.Show("登陆失败!!!");//异常提示
}
finally
{
my.Close();
cmd.Dispose();
my.Dispose();
}
}
}
}