您的位置:首页 > 其它

学生信息管理系统——登陆与注册界面解析

2012-12-27 10:26 459 查看
之前学习什么课程都是抱着明白的态度,总是给自己留着某天用的到再查。现在才发现这样的学习方法是错的!知识了解的再多不如掌握的实 。。。。。

这段时间,我们这些所谓的在校大学生和所有的学校的大学生都一样,以前玩的越开心的孩子,此刻是最忙的孩子,尤其是计算机专业的。我们的期末考试不是做卷子,而是上交毕业作品。。。我这个不算差的学生就很忙了,不过我还有时间给那些到现在还没有按软件或者配置环境的同学搭建开方环境,虽然这活我干烦了,但是同学一场,求到你了,你还能说什么??浪费了我多少时间??

好了,言归正传。。。。

先说说数据的连接吧。。起初我是用的:在每一个winform的窗口中都添加 引用:using System.Data.SqlClient;

string strConnect = "Data Source=CAI-PC\\SQLEXPRESS;Initial Catalog=MyData1;Persist Security Info=True;User ID=sa;Password=******";

这种连接方式不算是上策之方法 ,因为数据如果改变就要改每一个winform窗口,对于开发和后期应用的带来了很多不方便。。

我现在采用的是:新建一个独立的类,用来连接数据库。在新类头部分添加引用空间:suing System.Data.SqlClient;

我的代码如下:

namespace 学生信息管理系统
{
public class Datalink
{
public static String GetConnString()
{
SqlConnectionStringBuilder builder = new SqlConnectionStringBuilder();//实例化
builder.DataSource = ".";//本机数据库服务器
builder.InitialCatalog = "信息管理";//数据库名
builder.IntegratedSecurity = false;//登陆方式:fales为身份登陆,true为系统登陆
builder.UserID = "aab";//数据库用户
builder.Password = "123";//数据库用户密码
string connStr = builder.ToString();
return connStr;
}
}
}

然后在每个winform窗口添加语句:

    string strConnect = Datalink.GetConnString();
SqlConnection ole = new SqlConnection(strConnect);

  实现数据库连接。。。。

登陆就是sql查询数据,就是简单的数据对比,如下代码:

SqlCommand com = new SqlCommand("select 密码 from 用户 where 用户='" + txtnames.Text.Trim() + "'", ole);
string pwd = com.ExecuteScalar() as String;//ExecuteScalar方法从数据库中检索单个值,回传的必为 Object 类型,因此必须由程序员,手动将其强制转型为
//.NET 的 int 或 string 等想要的类型,以便直接指派给 int 或 string 类型的变量,或显示在页面上的控件中
ole.Close();
if (pwd == null)
{
MessageBox.Show("请输入正确的用户名");
txtnames.Text = txtpaws.Text = null;
}
else
{
pwd = pwd.Trim();
if (pwd == txtpaws.Text)
{
main frm = new main();
frm.Show();
this.Hide();
}
else
{
MessageBox.Show("请输入正确的密码");
txtpaws.Text = null;
}

登陆功能是写入数据库:

private void ok_Click(object sender, EventArgs e) { string username = txtname.Text.Trim(); string password1 = txtpaw.Text.Trim(); string password2 = txtpaw1.Text.Trim(); string mail = email.Text.Trim(); if (password1 == password2) { string strConnect = Datalink.GetConnString(); SqlConnection conn = new SqlConnection(strConnect); conn.Open(); SqlCommand comm = new SqlCommand("insert into 用户(用户,密码,E-mail) values('" + username + "','" + password1 + "','"+email+"')", conn); if (comm.ExecuteNonQuery() > 0) { MessageBox.Show("请登录"); } conn.Close();

} else { MessageBox.Show("确认密码有误"); txtpaw1.Text = null; } }
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: