额,偶对ASP.NET纯粹是打酱油的。呵呵,不过期末考试彭老师要我们做个期末考试的大作业。我不想忽悠彭老师,到网上直接下载一个给彭老师。于是,自己做了一些模块。记下来一下,呵呵。
首先第一个模块是一个关于验证码的生成。嘿嘿,这个其实我自己也没有看懂。但还是把代码贴一下,嘿嘿。
for (int i = 0; i < 4; i++)
{
number = random.Next();
if (number % 2 == 0)
code = (char)('0' + (char)(number % 10));
else
code = (char)('A' + (char)(number % 26));
checkCode += code.ToString();
}
Session["checkcode"] = checkCode;//保存验证码。
Response.Cookies.Add(new HttpCookie("CheckCode", checkCode));
return checkCode;
}
private void CreateCheckCodeImage(string checkCode)
{
if (checkCode == null || checkCode.Trim() == String.Empty)
return;
System.Drawing.Bitmap image = new System.Drawing.Bitmap((int)Math.Ceiling((checkCode.Length * 12.5)), 22);
Graphics g = Graphics.FromImage(image);
try
{
//生成随机生成器
Random random = new Random();
//清空图片背景色
g.Clear(Color.White);
//画图片的背景噪音线
for (int i = 0; i < 2; i++)
{
int x1 = random.Next(image.Width);
int x2 = random.Next(image.Width);
int y1 = random.Next(image.Height);
int y2 = random.Next(image.Height);
g.DrawLine(new Pen(Color.Black), x1, y1, x2, y2);
}
Font font = new System.Drawing.Font("Arial", 12, (System.Drawing.FontStyle.Bold | System.Drawing.FontStyle.Italic));
System.Drawing.Drawing2D.LinearGradientBrush brush = new System.Drawing.Drawing2D.LinearGradientBrush(new Rectangle(0, 0, image.Width, image.Height), Color.Blue, Color.DarkRed, 1.2f, true);
g.DrawString(checkCode, font, brush, 2, 2);
//画图片的前景噪音点
for (int i = 0; i < 100; i++)
{
int x = random.Next(image.Width);
int y = random.Next(image.Height);
image.SetPixel(x, y, Color.FromArgb(random.Next()));
}
//画图片的边框线
g.DrawRectangle(new Pen(Color.Silver), 0, 0, image.Width - 1, image.Height - 1);
System.IO.MemoryStream ms = new System.IO.MemoryStream();
image.Save(ms, System.Drawing.Imaging.ImageFormat.Gif);
Response.ClearContent();
Response.ContentType = "image/Gif";
Response.BinaryWrite(ms.ToArray());
}
finally
{
g.Dispose();
image.Dispose();
}
然后是登陆界面,并检查验证码是否正确……嘿嘿,这个模块自己还是写了一点东西……
}
private void land(string role)
{
//得到数据库链接字符串
DataCon datacon = new DataCon();
SqlConnection connection = datacon.getStrCon();
connection.Open();
SqlCommand com = connection.CreateCommand();
int count = 0;
if("student" == role)
{
com.CommandText = "select count(*) from student where NO='" + TBName.Text + "' and password='" + TBPas.Text + "'";
count = Convert.ToInt32(com.ExecuteScalar());//获取SQL语句的值 强制转换成数值类型
if(1 <= count)
{
Session.Add("studentNO", TBName.Text);
Page.Response.Redirect("student/stuInformation.aspx");
}
else
{
Response.Write("<mce:script lanuage=javascript><!--
alert('您的用户名或密码有误,请您重新输入!');location='javascript:history.go(-1)'
// --></mce:script>");
return;
}
}
else
{
com.CommandText = "select count(*) from admin where adminName ='" + TBName.Text + "' and adminPas='" + TBPas.Text + "'";
count = Convert.ToInt32(com.ExecuteScalar());
if (1 <= count)
{
Session.Add("adminName", TBName.Text);
Page.Response.Redirect("admin/allStudentInfor.aspx");
}
else
{
Response.Write("<mce:script lanuage=javascript><!--
alert('您的用户名或密码有误,请您重新输入!');location='javascript:history.go(-1)'
// --></mce:script>");
return;
}
}
connection.Close();
}
学生信息的显示界面
学生密码的修改。
}
else//如果旧密码错误
{
Response.Write("<mce:script language=javascript><!--
alert('您输入的密码错误,检查后重新输入!')
// --></mce:script>");
}
}
catch (System.Exception ee)//进行异常处理
{
Response.Write("<mce:script language=javascript><!--
alert('" + ee.Message.ToString() + "')
// --></mce:script>");
}
finally
{
connection.Close();//关闭数据库
}
管理员添加学生
// 添加参数
foreach (SqlParameter parameter in prams)
{
cmd.Parameters.Add(parameter);
}
cmd.ExecuteNonQuery();
con.Close();
Response.Write("<mce:script type="text/javascript"><!--
alert('信息添加成功!')
// --></mce:script>");
}