数据库操作(c#)
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了数据库操作(c#),小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2268字,纯文字阅读大概需要4分钟。
内容图文
windows窗体程序中的数据库操作部分
//数据库连接串 internal static string connstring = "Data Source = 192.168.1.1; Initial Catalog = testdb; User ID = test; pwd = 111111;";
//学生登录 using (SqlConnection conn = new SqlConnection(login_mainform.connstring)) { string sql = "select * from test_student where name=@name and pwd=@pwd"; SqlCommand cmd = new SqlCommand(sql, conn); cmd.Parameters.Add(new SqlParameter("@name", txtusername.Text)); cmd.Parameters.Add(new SqlParameter("@pwd", txtpassword.Text)); conn.Open(); SqlDataReader reader = cmd.ExecuteReader(); Student stu = new Student(); if (reader.Read()) { stu.Sno = reader[0].ToString(); stu.Name = reader[1].ToString(); stu.Major = reader[2].ToString(); stu.Birthday = reader[3].ToString(); stu.Sex = (reader[4].ToString())=="1"?"男":"女"; Information info = new Information(stu); //新窗口对象 info.Show(); //显示学生信息 this.Hide(); //隐藏当前窗口 } else { MessageBox.Show("输入有误"); } #endregion }
//修改数据 using (SqlConnection conn = new SqlConnection(login_mainform.connstring)) { string sql = "update test_student set pwd=@pwd where sno=@sno"; SqlCommand cmd = new SqlCommand(sql,conn); cmd.Parameters.Add(new SqlParameter("@pwd", txtnewpwd.Text)); cmd.Parameters.Add(new SqlParameter("@sno", txtsno.Text)); conn.Open(); int rows= cmd.ExecuteNonQuery(); if (rows>0) { MessageBox.Show("密码修改成功,请重新登录"); login_mainform form = new login_mainform(); //登录窗口 form.Show(); this.Close(); } else MessageBox.Show("存储出错"); }
//连接数据库查询符合查询条件的数据 adapter using (SqlConnection conn = new SqlConnection(login_mainform.connstring)) { conn.Open(); String sql =String.Format("select sno,name,sex,birthday,major from test_student where name like '%{0}%' and major like '%{1}%' and sno like '%{2}%'", txtnamequery.Text,cmbmajor.Text,txtsnoquery.Text); SqlDataAdapter da = new SqlDataAdapter(sql,conn); DataSet ds = new DataSet(); da.Fill(ds); }
//更新密码 using (SqlConnection conn = new SqlConnection(login_mainform.connstring)) { conn.Open(); string sno = dgvstudent.CurrentRow.Cells[0].Value.ToString(); String sql = String.Format("update test_student set pwd = '123456' where sno = {0}", sno); SqlCommand cmd = new SqlCommand(sql, conn); int rows = cmd.ExecuteNonQuery(); if (rows > 0) MessageBox.Show("重置成功。密码:123456"); else return; }
内容总结
以上是互联网集市为您收集整理的数据库操作(c#)全部内容,希望文章能够帮你解决数据库操作(c#)所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。