终于等到这样的一天啦,嘿嘿
嘿嘿,今天是蛮高兴的啦,这个还要源于我们上课的进度啦,从开始学习到现在终于可以做一个简单的小程序啦,之前在一个公司上班一个月啦,那时间真的看到好多的关键字就是讨厌,没有看到过,所以不认识啦,在学校可能也讲过吧,但是那时间讲的就是简单地一下划过啦,今天我们学习啦自己写SqlHelp这个类的,就是把昨天学习的ADO.net中好多的方法封装成一个类啦,然后我们仅仅需要调用一下就可以啦,不用每次使用每次写一个方法啦,因为我昨天还没学习,所以就写在今天啦,友友们也给我说啦,让我封装成一个类啦,那我就总结一下啦,嘿嘿。
一.SqlHelp类的封装
static class SqlHelp { static string connection = "Data Source=.;Initial Catalog=Student;Integrated Security=True"; public static int ExcuteNonQuery(string sql, params SqlParameter[] sp) { using (SqlConnection conn = new SqlConnection(connection)) { conn.Open(); using (SqlCommand cmd = new SqlCommand(sql, conn)) { cmd.CommandText = sql; cmd.Connection = conn; cmd.Parameters.AddRange(sp); return cmd.ExecuteNonQuery(); } } } public static int ExcuteScalre(string sql, params SqlParameter[] sp) { using (SqlConnection conn = new SqlConnection(connection)) { conn.Open(); using (SqlCommand cmd = new SqlCommand(sql, conn)) { cmd.Parameters.AddRange(sp); return cmd.ExecuteNonQuery(); } } } public static DataTable ExcuteDataTable(string sql, params SqlParameter[] sp) { using (SqlConnection conn = new SqlConnection(connection)) { conn.Open(); using (SqlCommand cmd = new SqlCommand(sql, conn)) { cmd.Parameters.AddRange(sp); DataTable dt = new DataTable(); using (SqlDataAdapter adapter = new SqlDataAdapter(cmd)) { adapter.Fill(dt); } return dt; } } } public static SqlDataReader ExcuteSqlDataReader(string sql, params SqlParameter[] sp) { SqlConnection conn = new SqlConnection(connection); conn.Open(); SqlCommand cmd = new SqlCommand(sql, conn); cmd.Parameters.AddRange(sp); SqlDataReader reader = cmd.ExecuteReader(CommandBehavior.CloseConnection); return reader; } }
二.熟练运用SqlHelp类
在熟悉使用SqlHelp这个类,下面我做啦一个小程序啦,就是围绕调用SqlHelp类中的方法的熟练使用写的,下面总结一下啦。
1.建立了三个窗体,分别为Form1,Login登陆页面,Register注册页面,当选中ListBox中的任意一项,点击下方的按钮就可以实现其功能,在这里,Register页面实现啦两个功能,一个是直接的注册页面,一个是修改页面的功能,嘿嘿,窗体样式如下:
2.实现其代码如下:
public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { string sql = "select * from Register"; DataTable dt = SqlHelp.ExcuteDataTable(sql); foreach (DataRow item in dt.Rows) { string format = string.Format("{0},{1},{2},{3},{4},{5},{6}\n\t", item[0], item[1], item[2], item[3], item[4], item[5], item[6]); this.listBox1.Items.Add(format); } } private void btndel_Click(object sender, EventArgs e) { int id =int.Parse(this.listBox1.SelectedItem.ToString().Substring(0,1)); string sql = "delete from Register where id=@id"; SqlParameter sp = new SqlParameter("@id",id); if (SqlHelp.ExcuteNonQuery(sql, sp) > 0) { MessageBox.Show("删除成功"); } else { MessageBox.Show("删除失败"); } } private void butadd_Click(object sender, EventArgs e) { Register r = new Register(); r.Show(); this.Hide(); } private void btnupd_Click(object sender, EventArgs e) { int id = int.Parse(this.listBox1.SelectedItem.ToString().Substring(0, 1)); string sql = "select * from Register where id=@id"; SqlParameter sp = new SqlParameter("@id",id); SqlDataReader reader = SqlHelp.ExcuteSqlDataReader(sql, sp); if (reader.Read()) { string d = reader[0].ToString(); string name = reader[1].ToString(); string pwd = reader[2].ToString(); string age = reader[3].ToString(); string sex = reader[4].ToString(); string address = reader[5].ToString(); string phone = reader[6].ToString(); Register r = new Register(d,name,pwd,age,sex,address,phone); r.Show(); r.Text = "修改"; r.Btnreg.Text = "更新"; this.Hide(); } } }
public partial class Register : Form { public Register() { InitializeComponent(); } bool b = false;//默认是新增 string id = string.Empty; public Register(string id, string username, string pwd, string sex, string address, string phone, string age) { InitializeComponent(); b = true; this.txtUserName.Text = username; this.txtpwd.Text = pwd; this.txtphone.Text = phone; string birthday = this.dateTimePicker1.Text; this.txtaddress.Text = address; this.id = id; if (sex.ToLower().Equals("false")) { this.radioButton2.Checked = true; } else { this.radioButton1.Checked = true; } //int age = DateTime.Now.Year - DateTime.Parse(birthday).Year; } private void btnreg_Click(object sender, EventArgs e) { string username = this.txtUserName.Text; string pwd = this.txtpwd.Text; string address = this.txtaddress.Text; string birthday = this.dateTimePicker1.Text; string phone = this.txtphone.Text; bool sex = this.radioButton1.Checked ? true : false; int age = DateTime.Now.Year - DateTime.Parse(birthday).Year; //通过输入生日,计算出该年龄 if (!b) { MD5 md5 = new MD5CryptoServiceProvider(); //创建MD5的加密对象 byte[] bytes = Encoding.Default.GetBytes(pwd);//把要进行MD5加密的字符串转换成字节数组 byte[] bytess = md5.ComputeHash(bytes); pwd = BitConverter.ToString(bytess).Replace("-", ""); if (!string.IsNullOrEmpty(username) && !string.IsNullOrEmpty(pwd) && !string.IsNullOrEmpty(birthday) && !string.IsNullOrEmpty(address) && !string.IsNullOrEmpty(phone)) { string sql = "insert into Register(UserName,Pwd,Age,Address,Phone,Sex)values(@username,@pwd,@age,@address,@phone,@sex)"; SqlParameter[] sp ={ new SqlParameter("@username",username), new SqlParameter("@pwd",pwd), new SqlParameter("@age",age), new SqlParameter("@sex",sex), new SqlParameter("@address",address), new SqlParameter("@phone",phone), }; if (SqlHelp.ExcuteNonQuery(sql, sp) > 0) { MessageBox.Show("注册成功"); } else { MessageBox.Show("注册失败"); } } else { MessageBox.Show("信息不能为空"); } } else { string sql = "update Register set UserName=@username,pwd=@pwd,age=@age,sex=@sex,address=@address,phone=@phone where id=@id"; SqlParameter[] sp = { new SqlParameter("@username", username), new SqlParameter("@pwd", pwd), new SqlParameter("@age", age), new SqlParameter("@sex", sex), new SqlParameter("@address", address), new SqlParameter("@phone", phone), new SqlParameter("@id", id) }; if (SqlHelp.ExcuteNonQuery(sql, sp) > 0) { MessageBox.Show("更新成功"); } else { MessageBox.Show("更新失败"); } } } }
public partial class Login : Form { public Login() { InitializeComponent(); } private void btnlog_Click(object sender, EventArgs e) { string name = this.txtname.Text.Trim(); string pwd = this.textBox1.Text.Trim(); MD5 md5 = new MD5CryptoServiceProvider();//创建MD5加密对象 byte[] bytes = Encoding.Default.GetBytes(pwd);//把要进行MD5加密的字符串转换成字节数组 byte[] bytess = md5.ComputeHash(bytes); pwd = BitConverter.ToString(bytess).Replace("-",""); string sql = "select count(*) from Register where UserName=@name and pwd=@pwd"; SqlParameter[] sp={ new SqlParameter("@name",name), new SqlParameter("@pwd",pwd) }; if (SqlHelp.ExcuteScalre(sql, sp) > 0) { MessageBox.Show("登陆成功"); } else { MessageBox.Show("登录失败"); } } }
嘿嘿,这个小程序就是这样的,或许已经做过好久程序的友友会说,这样的小程序也能说出来啊,但是我感觉我还是开始,每个人都会有一个开始,我这样的小小激动还是会存在的的。突然想起啦今天上课时间老师问:“做程序感觉怎么样”?我的第一反应就是不错额,我感觉做出来啦程序就是很有成就感啊,虽然现在还谈不上什么成果啦,但是我会努力的,之前我没想一直在这个领域发展的,但是从我开始接触程序,我发现我就是和它是好朋友啦,感觉做程序就是深深的吸引着我,所以我要努力学习,不论结果怎么样,这个小的开始我不能落后啦,今天我可能遇到啦好多麻烦啦,博客园里面的友友都很热情啦,还能帮我解决问题,嘿嘿,灰常的感谢啦,在这里,我会每天总结自己学习的,只希望我会努力坚持学好.Net语言。