using System; using System.Windows.Forms; using System.Data.SqlClient; using System.IO; namespace _8登陆练习操作 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void SetErrorTimes() { using (SqlConnection conn = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=E:\MyProjects\C#net传智播客\第五季ADO.NET\8登陆练习操作\8登陆练习操作\Database1.mdf;Integrated Security=True;User Instance=True")) { conn.Open(); using (SqlCommand upComd = conn.CreateCommand()) { upComd.CommandText = "update T_User set ErrorTimes = ErrorTimes + 1 WHERE UserName=@UserName"; upComd.Parameters.Add(new SqlParameter("UserName", UserName.Text)); upComd.ExecuteNonQuery(); } } } private void ResetErrorTimes() { using (SqlConnection conn = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=E:\MyProjects\C#net传智播客\第五季ADO.NET\8登陆练习操作\8登陆练习操作\Database1.mdf;Integrated Security=True;User Instance=True")) { conn.Open(); using (SqlCommand updateCmd = conn.CreateCommand()) { updateCmd.CommandText = "update T_User set ErrorTimes = 0 WHERE UserName=@UserName"; updateCmd.Parameters.Add(new SqlParameter("UserName", UserName.Text)); updateCmd.ExecuteNonQuery(); } } } private void button1_Click(object sender, EventArgs e) { using (SqlConnection conn = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=E:\MyProjects\C#net传智播客\第五季ADO.NET\8登陆练习操作\8登陆练习操作\Database1.mdf;Integrated Security=True;User Instance=True")) { conn.Open(); using (SqlCommand cmd = conn.CreateCommand()) { cmd.CommandText = "Select * FROM T_User WHERE UserName=@UserName"; cmd.Parameters.Add(new SqlParameter("UserName", UserName.Text)); using (SqlDataReader reader = cmd.ExecuteReader()) { if (reader.Read()) { int errotTimes = reader.GetInt32(reader.GetOrdinal("ErrorTimes")); if (errotTimes >= 3) { //Console.WriteLine("对不起用户已经被锁定!"); MessageBox.Show("对不起用户已经被锁定!"); } else{ string passwd = Password.Text; string t_passwd =reader.GetString(reader.GetOrdinal("Password")); if (passwd == t_passwd) { //清空一下ErrorTimes //这里还没有其它的办法,只能另外写一个函数用于来更新ErrorTimes的值 /*cmd.Parameters.Clear(); cmd.CommandText = "update T_User set ErrorTimes = 0 WERE UserName=@UserName"; cmd.Parameters.Add(new SqlParameter("UserName", UserName.Text)); cmd.ExecuteNonQuery(); * */ ResetErrorTimes(); MessageBox.Show("登陆成功!"); //Console.WriteLine("登陆成功!"); } else { //添加ErrorTimes /*cmd.Parameters.Clear(); //这里选清空一下Parameters cmd.CommandText = "update T_User set ErrorTimes = ErrorTimes + 1 WHERE UserName=@UserName"; cmd.Parameters.Add(new SqlParameter("UserName",UserName.Text)); cmd.ExecuteNonQuery(); */ //在同一个连接中,如果SqlDataReader没关闭,那么是不能执行Update之类的语句的 SetErrorTimes(); MessageBox.Show("密码错误!"); //Console.WriteLine("密码错误!"); } } } else { //Console.WriteLine("用户名不存在!"); MessageBox.Show("用户名不存在!"); } } } } } //文件导入 private void button2_Click(object sender, EventArgs e) { //当点击弹出窗口,然后点击了确定 if (openFileDialog1.ShowDialog() == DialogResult.OK) { //取得文件流 using (FileStream fileStream = File.OpenRead(openFileDialog1.FileName)) { //读取文件 using (StreamReader streamReader = new StreamReader(fileStream)) { string line = null; while ((line = streamReader.ReadLine()) != null) { string[] str = line.Split('|'); string name = str[0]; string passwd = str[1]; int errorTimes = Convert.ToInt32(str[2]); using (SqlConnection conn = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=E:\MyProjects\C#net传智播客\第五季ADO.NET\8登陆练习操作\8登陆练习操作\Database1.mdf;Integrated Security=True;User Instance=True")) { conn.Open(); using (SqlCommand cmd = conn.CreateCommand()) { //MessageBox.Show("name:"+name+",passwd:"+passwd+",errorTimes:"+errorTimes+""); //在这里测试了一会儿,唉,自己不小心,把变量给写错了,看来以后还要多加小心才行 cmd.CommandText = "Insert into T_User(UserName, Password, ErrorTimes) values(@username,@password,@errortimes) "; cmd.Parameters.Add(new SqlParameter("username", name)); cmd.Parameters.Add(new SqlParameter("password", passwd)); cmd.Parameters.Add(new SqlParameter("errortimes", errorTimes)); cmd.ExecuteNonQuery(); } } } } } } MessageBox.Show("导入成功!"); } //文件导出 private void button3_Click(object sender, EventArgs e) { using (SqlConnection conn = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=E:\MyProjects\C#net传智播客\第五季ADO.NET\8登陆练习操作\8登陆练习操作\Database1.mdf;Integrated Security=True;User Instance=True")) { conn.Open(); using (SqlCommand cmd = conn.CreateCommand()) { cmd.CommandText = "select * FROM T_User"; //读取多行数据 using (SqlDataReader reader = cmd.ExecuteReader()) { StreamWriter sw = File.CreateText(@"c:\2.txt"); while (reader.Read()) //循环数据 { long id = reader.GetInt64(reader.GetOrdinal("Id")); string name = reader.GetString(reader.GetOrdinal("UserName")); string passwd = reader.GetString(reader.GetOrdinal("Password")); int errortimes = reader.GetInt32(reader.GetOrdinal("ErrorTimes")); sw.WriteLine("{0}|{1}|{2}|{3}",id,name,passwd,errortimes); //MessageBox.Show("id:" + id + ",name:" + name + ",passwd:" + passwd + ",errortimes:" + errortimes); } sw.Close(); } } } MessageBox.Show("导出数据成功!"); } /*省市选择程序,数据全部来自于数据库:http://www.programfan.com/blog/article.asp?id=28128 * 把createtable中的varchar改为nvarchar,在insert语句的汉字前面加上N(查找","替换为".N")我不做好像也没事 * ComboBox的显示值,Items.Add的参数是Object类型,也就是可以放任意数据类型的数据 * 可以设置DisplayMember属性设定显示的属性,通过SelectItem属性取得到就是选择的条目对应的对像,例子 * 疑问: 取出来的是Object,怎么能转换为对应的类型?变量名只是"标签".显示的值和实际的对像不一样,在ASP.Net中也有相同的东西 * 创建一个ProvinceItem类,将数据填充在这个对像中添加到ComBoBox中 * * */ private void Form1_Load(object sender, EventArgs e) { //MessageBox.Show("一进入就开始加载省份信息!"); //设定comboBox的DisplayMember属性为name,就会显示对像的Name属性的值 /*cb_city.DisplayMember = "Name"; ProvinceItem item = new ProvinceItem(); item.Id = 1; item.Name = "北京"; cb_city.Items.Add(item); ProvinceItem item2 = new ProvinceItem(); item2.Id = 2; item2.Name = "上海"; cb_city.Items.Add(item2);*/ using (SqlConnection conn = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=E:\MyProjects\C#net传智播客\第五季ADO.NET\8登陆练习操作\8登陆练习操作\Database1.mdf;Integrated Security=True;User Instance=True")) { conn.Open(); using (SqlCommand cmd = conn.CreateCommand()) { cmd.CommandText = "select * FROM promary "; cb_city.DisplayMember = "Name"; using (SqlDataReader reader = cmd.ExecuteReader()) { while (reader.Read()) { int proid = reader.GetInt32(reader.GetOrdinal("proID")); string proName = reader.GetString(reader.GetOrdinal("proName")); ProvinceItem item = new ProvinceItem(); item.Id = proid; item.Name = proName; cb_city.Items.Add(item); } } } } } private void cb_city_SelectedIndexChanged(object sender, EventArgs e) { /*string text = cb_city.SelectedText; int index = cb_city.SelectedIndex; //string value = cb_city.SelectedValue; string item = cb_city.SelectedItem.ToString(); MessageBox.Show("item=" + item);*/ ProvinceItem item = (ProvinceItem)cb_city.SelectedItem; int proId = item.Id; string name = item.Name; //MessageBox.Show("proId:"+proId+",name:"+name); using (SqlConnection conn = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=E:\MyProjects\C#net传智播客\第五季ADO.NET\8登陆练习操作\8登陆练习操作\Database1.mdf;Integrated Security=True;User Instance=True")) { conn.Open(); using (SqlCommand cmd = conn.CreateCommand()) { cmd.CommandText = "select * FROM city WHERE proID="+proId; using (SqlDataReader reader = cmd.ExecuteReader()) { cb_city_small.Items.Clear(); cb_city_small.SelectedIndex = -1; cb_city_small.DisplayMember = "Name"; while (reader.Read()) { ProvinceItem city = new ProvinceItem(); int cityID = reader.GetInt32(reader.GetOrdinal("cityID")); string cityName = reader.GetString(reader.GetOrdinal("cityName")); city.Id = cityID; city.Name = cityName; cb_city_small.Items.Add(city); } } } } } } class ProvinceItem { public string Name { get; set; } public int Id { get; set; } } }