本次作业的主要难点以及问点就是在于数据库的链接上
首先,创建一个新的项目,这就省略到了,上一篇有详细介绍,我的项目名字叫LoginDemo
然后,设计登录的页面,就是添加标签、文本框以及按钮等控件。
在描写连接数据库之前,我们要先在数据库中建立一个登录表,里面存放登录名以及密码。
在几个控件中我们最重要的“ok”这个按钮,因此我们的代码集中于“ok”按钮所展开的。而我们的连接数据库代码篇也是在这个按钮里面描述的。
ok按钮
这块的代码主要就是描述数据库的连接,是固定格式,理解加记忆之后就能掌握。
使用之前,我们要先声明
using System.Data.SqlClient;
然后具体代码
private void button1_Click(object sender, EventArgs e)
{
string username = textBoxUserName.Text.Trim(); //取出账号
string password = textBoxPassWord.Text.Trim(); //取出密码
//string connstr = ConfigurationManager.ConnectionStrings["connectionString"].ToString(); //读取连接字符串
string myConnString = "Data Source=.;Initial Catalog=Test;Persist Security Info=True;User ID=sa;Password=*****";//连接数据库
SqlConnection sqlConnection = new SqlConnection(myConnString); //实例化连接对象
sqlConnection.Open();
string sql = "select loginname,loginpassword from login where loginname = '" + username + "' and loginpassword = '" + password + "'"; //编写SQL命令
SqlCommand sqlCommand = new SqlCommand(sql, sqlConnection);
SqlDataReader reader