|
代码:
using System; 
using System.Collections.Generic;
using System.ComponentModel; 
using System.Data; 
using System.Data.OleDb; 
using System.Drawing; 
using System.Linq; 
using System.Text; 
using System.Windows.Forms; 
using System.Data.SqlClient;
using System.Configuration;
using System.IO; 
namespace 用户登录
{
public partial class library_log : Form
{
public library_log()
{
InitializeComponent();
}
private void bt_log_Click(object sender, EventArgs e)
{
string userLoginName = tx_username.Text;
string userPassword = tx_password.Text;
//新建数据库连接conn,连接到SQL Sever 数据库
System.Data.SqlClient.SqlConnection conn = new SqlConnection();
conn.ConnectionString = ConfigurationManager.ConnectionStrings["LibraryConnectionString"].ConnectionString;
conn.Open();
SqlCommand cmd = conn.CreateCommand();
cmd.CommandText = "SELECT *FROM[Readers] where R_username = @R_username";
SqlDataReader dr = cmd.ExecuteReader();
if (dr.Read())
{
if (dr["R_userpassword"].ToString().Trim() == userPassword)
{
MessageBox.Show("登录成功", "Welcome!", MessageBoxButtons.OK, MessageBoxIcon.Information);
this.Visible = false;
new library().Show();
}
else
{
this.tx_password.Text = "";
MessageBox.Show("密码错误,请重新输入!", "错误", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
else
{
tx_username.Text = "";
tx_password.Text = "";
MessageBox.Show("用户名不存在,请注册!", "注册", MessageBoxButtons.OK, MessageBoxIcon.Information);
conn.Close();
dr.Close();
}
}
}
}
配置文件:
<configuration>
<connectionStrings>
<add name="LibraryConnectionString"
connectionString="Data Source=thinkpad-think\sqlexpress; Initial Catalog=Library;R_username=sa; Password=123"
providerName="System.Data.SqlClient" />
</connectionStrings>
</configuration>
= =然后R_username错误- -这个是不是就是用的数据库里的字段呢?
然后代码里应该还有些问题- -求教 |
|