1. private void btnLogin_Click(object sender, EventArgs e)
  2. {
  3. string txtUserName = this.txtUserName.Text.Trim();
  4. string txtPwd = this.txtPwd.Text.Trim();
  5. if (txtUserName==null||txtPwd==null||txtUserName.Length==||txtPwd.Length==)
  6. {
  7. MessageBox.Show("您输入的内容为空,请重新输入!");
  8. }
  9. string connString = "server=.;database=StudentMISDB;uid=sa;pwd=123456";
  10. SqlConnection conn = new SqlConnection(connString);
  11. conn.Open();
  12. string sql = @"select l.*,DATEDIFF(MI,LoginErrorLastTime,GETDATE()) as 间隔 from login as l where loginname='{0}'";
  13. sql = string.Format(sql, txtUserName);
  14. SqlCommand cmd = new SqlCommand(sql,conn);
  15. SqlDataAdapter adapter = new SqlDataAdapter(cmd);
  16. DataSet dSet = new DataSet();
  17. adapter.Fill(dSet);
  18. conn.Close();
  19. if (dSet.Tables[].Rows.Count > )
  20. {
  21. int errorCount = Convert.ToInt32(dSet.Tables[].Rows[][]);
  22. int times = Convert.ToInt32(dSet.Tables[].Rows[][]);
  23. if (errorCount >= && times <= )
  24. {
  25. if (dSet.Tables[].Rows[][].ToString() == txtUserName && dSet.Tables[].Rows[][].ToString()==txtPwd)
  26. {
  27. MessageBox.Show("登陆成功");
  28. conn.Open();
  29. string uptateSql = @"update login set loginerrorcount=0 where id='{0}'";
  30. uptateSql = string.Format(uptateSql,dSet.Tables[].Rows[][].ToString());
  31. cmd = new SqlCommand(uptateSql,conn);
  32. cmd.ExecuteNonQuery();
  33. conn.Close();
  34. }
  35. else
  36. {
  37. MessageBox.Show("登录名或者密码错误!");
  38. conn.Open();
  39. string updateSql = @"update login set loginerrorcount=loginerrorcount+1 ,loginerrorlasttime=getdate() where id='{0}'";
  40. updateSql = string.Format(updateSql,dSet.Tables[].Rows[][].ToString());
  41. cmd = new SqlCommand(updateSql,conn);
  42. cmd.ExecuteNonQuery();
  43. conn.Close();
  44. }
  45. }
  46. else
  47. {
  48. MessageBox.Show("请在"+(-times)+"分钟后登录!");
  49. }
  50. }
  51. else
  52. {
  53. MessageBox.Show("用户不存在!");
  54. }
  55. }
  56. }
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. using System.Windows.Forms;
  10. using System.Data.SqlClient;
  11.  
  12. namespace ADO.NET8._30
  13. {
  14. public partial class DataGridView : Form
  15. {
  16. public DataGridView()
  17. {
  18. InitializeComponent();
  19. }
  20.  
  21. private void DataGridView_Load(object sender, EventArgs e)
  22. {
  23. //加载学生选课信息
  24. loadStudntData();
  25. //加载课程信息(没有选的课程)
  26. LoadCourse();
  27. }
  28. /// <summary>
  29. /// 自定义一个方法
  30. /// </summary>
  31. private void LoadCourse()
  32. {
  33. //1.创建数据库连接符
  34. string connString = "server=.;database=StudentMISDB;uid=sa;pwd=123456";
  35. //2.链接数据库
  36. SqlConnection conn = new SqlConnection(connString);
  37. //3.添加数据库要执行的语句,通过ID查找没有选择到的课程
  38. string sql = @"select * from Course where CourseId not in(select distinct sc.CourseId from [dbo].[Students] as s
  39. join Score as sc on s.StudentId=sc.StudentId
  40. join Course as c on sc.CourseId = c.CourseId
  41. where s.StudentId='2')";
  42. //4.创建命令
  43. SqlCommand cmd = new SqlCommand(sql, conn);
  44. //5.断开式连接查询
  45. SqlDataAdapter da = new SqlDataAdapter(cmd);
  46. //6.创建数据缓冲区(数据集)
  47. DataSet ds = new DataSet();
  48. conn.Open();
  49. //7.填充数据集
  50. da.Fill(ds);
  51. conn.Close();
  52. //8.绑定数据源
  53. this.cmbCourseName.DataSource = ds.Tables[];
  54.  
  55. //9.设置combobox控件中要显示的列
  56. //this.cmboxCourse.DisplayMember = "列名";
  57. this.cmbCourseName.DisplayMember = "Name";
  58. //10.DisplayMember绑定需要显示的数据表字段,而ValueMember绑定需要获取选择的项的值;直接可见的是此item的 DisplayMember 对应内容,而此 item的值是ValueMember 的对应内容。
  59. this.cmbCourseName.ValueMember = "CourseId";
  60. }
  61. /// <summary>
  62. /// 获取选中的值
  63. /// </summary>
  64. /// <param name="sender"></param>
  65. /// <param name="e"></param>
  66. private void cmbCourseName_SelectedIndexChanged(object sender, EventArgs e)
  67. {
  68. string courseID = this.cmbCourseName.SelectedValue.ToString();
  69. ///string courseName = this.cmboxCourse.SelectedText;
  70. }
  71. /// <summary>
  72. /// 保存 选课后,
  73. /// </summary>
  74. /// <param name="sender"></param>
  75. /// <param name="e"></param>
  76. private void btnSave_Click(object sender, EventArgs e)
  77. {
  78. //获取到在combobox控件中已经选择的值
  79. string courseID = this.cmbCourseName.SelectedValue.ToString();
  80. //string courseName = this.cmboxCourse.SelectedText;
  81. int studentId = ;
  82. //1.
  83. string connString = "server=.;database=StudentMISDB;uid=sa;pwd=123456";
  84. SqlConnection conn = new SqlConnection(connString);
  85.  
  86. string sql = "insert into Score values('{0}','{1}','{2}')";
  87. sql = string.Format(sql, studentId, courseID, );
  88.  
  89. SqlCommand cmd = new SqlCommand(sql, conn);
  90. conn.Open();
  91. int result = cmd.ExecuteNonQuery();
  92. conn.Close();
  93. if (result > )
  94. {
  95. MessageBox.Show("保存成功");
  96.  
  97. loadStudntData();
  98. LoadCourse();
  99. }
  100. else
  101. {
  102. MessageBox.Show("保存失败");
  103. }
  104. }
  105. /// <summary>
  106. /// 加载学生选课信息
  107. /// </summary>
  108. private void loadStudntData()
  109. {
  110. string connString = "server=.;database=StudentMISDB;uid=sa;pwd=123456";
  111. SqlConnection conn = new SqlConnection(connString);
  112.  
  113. string sql = @"select s.Studentid,c.courseId, s.Name as 姓名,c.Name as 课程名, sc.Score as 成绩 from [dbo].[Students] as s
  114. join Score as sc on s.StudentId=sc.StudentId
  115. join Course as c on sc.CourseId = c.CourseId
  116. where s.StudentId='{0}'";
  117. sql = string.Format(sql, );
  118.  
  119. SqlCommand cmd = new SqlCommand(sql, conn);
  120. SqlDataAdapter da = new SqlDataAdapter(cmd);
  121. DataSet ds = new DataSet();
  122. conn.Open();
  123. da.Fill(ds);
  124. conn.Close();
  125.  
  126. this.dataGridView1.DataSource = ds;
  127. this.dataGridView1.DataMember = ds.Tables[].TableName;
  128. }
  129. }
  130. }

二、(1)封装的类:

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Data.SqlClient;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. using System.Data;
  8.  
  9. namespace Demo2
  10. {
  11. public static class SqlCommon
  12. {
  13.  
  14. /// <summary>
  15. /// 连接字符串
  16. /// </summary>
  17. public static string connString = "server=.;database=StudentMISDB;uid=sa;pwd=123456";
  18.  
  19. /// <summary>
  20. /// 执行增删改操作
  21. /// </summary>
  22. /// <param name="sql">sql语句 参数传入</param>
  23. /// <returns></returns>
  24. public static int ExecuteSql(string sql)
  25. {
  26. int result = ;
  27. //创建连接对象new SqlConnection( 连接字符串)
  28. SqlConnection conn = new SqlConnection(connString);
  29. //创建命令对象 new SqlCommand(sql语句, conn)
  30. SqlCommand cmd = new SqlCommand(sql, conn);
  31. // 打开数据连接
  32. conn.Open();
  33. // 执行 sql 命令,返回受影响的行数
  34. result = cmd.ExecuteNonQuery();
  35. // 关闭数据连接
  36. conn.Close();
  37. // 把执行结果【受影响的行数】,返回给调用者
  38. return result;
  39. }
  40.  
  41. public static DataSet ExecuteQuery(string sql) {
  42.  
  43. SqlConnection conn = new SqlConnection(connString);
  44. SqlCommand cmd = new SqlCommand(sql, conn);
  45. SqlDataAdapter da = new SqlDataAdapter(cmd);
  46. DataSet ds = new DataSet();
  47. conn.Open();
  48. da.Fill(ds);
  49. conn.Close();
  50.  
  51. return ds;
  52. }
  53. }
  54. }

(二)、调用类

  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. using System.Windows.Forms;
  10.  
  11. using System.Data.SqlClient;
  12.  
  13. namespace Demo2
  14. {
  15. public partial class Form1 : Form
  16. {
  17. public Form1()
  18. {
  19. InitializeComponent();
  20. }
  21.  
  22. private void btnAdd_Click(object sender, EventArgs e)
  23. {
  24. /// 定义变量接收 用户输入的值
  25. string loginName = this.txtLoginName.Text.Trim();
  26. string pwd = this.txtPWD.Text.Trim();
  27.  
  28. ///判断用户是否有输入值
  29. if (loginName == "" || loginName == null)
  30. {
  31. MessageBox.Show("请输入用户名");
  32. return;
  33. }
  34. //string.IsNullOrEmpty(字符串) ==>判断字符串是否为“空字符”或为 null
  35. if (string.IsNullOrEmpty(pwd))
  36. {
  37. MessageBox.Show("请输入密码");
  38. return;
  39. }
  40.  
  41. if (isExistsLoginName(loginName))
  42. {
  43. MessageBox.Show("该用户名已经存在,请重新输入");
  44. this.txtLoginName.Text = string.Empty;
  45. this.txtPWD.Text = string.Empty;
  46. return;
  47. }
  48.  
  49. string sql = @"insert into LoginInfo (loginName,pwd,LoginErrorLastTime)
  50. values('{0}','{1}','{2}')";
  51. sql = string.Format(sql, loginName, pwd, DateTime.Now.ToString());
  52.  
  53. int row = SqlCommon.ExecuteSql(sql);
  54. if (row > )
  55. {
  56. MessageBox.Show("添加成功");
  57. }
  58. else
  59. {
  60. MessageBox.Show("添加失败");
  61. }
  62.  
  63. }
  64.  
  65. #region 判断 用户名是否存在
  66.  
  67. private bool isExistsLoginName(string loginName)
  68. {
  69. string sql = @"select * from LoginInfo where LoginName='{0}'";
  70. sql = string.Format(sql, loginName);
  71. DataSet ds = SqlCommon.ExecuteQuery(sql);
  72. return ds.Tables[].Rows.Count > ;
  73. }
  74. #endregion
  75. }
  76. }

(三)、datagrideview插件

  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. using System.Windows.Forms;
  10.  
  11. namespace Demo2
  12. {
  13. public partial class FrmScore : Form
  14. {
  15. public FrmScore()
  16. {
  17. InitializeComponent();
  18. }
  19.  
  20. private void btnSelect_Click(object sender, EventArgs e)
  21. {
  22. string name = this.txtCourse.Text.Trim();
  23.  
  24. string sql = @"select ScoreId,Name,Score from Course as c
  25. join Score as sc on c.CourseId = sc.CourseId
  26. where sc.StudentId='1' and c.Name like '%{0}%'";
  27. sql = string.Format(sql,name);
  28. DataSet ds = SqlCommon.ExecuteQuery(sql);
  29.  
  30. this.dataGridView1.DataSource = ds.Tables[];
  31.  
  32. }
  33. }
  34. }

C# datagrideview插件的使用的更多相关文章

  1. Angular杂谈系列1-如何在Angular2中使用jQuery及其插件

    jQuery,让我们对dom的操作更加便捷.由于其易用性和可扩展性,jQuer也迅速风靡全球,各种插件也是目不暇接. 我相信很多人并不能直接远离jQuery去做前端,因为它太好用了,我们以前做的东西大 ...

  2. Jenkins 安装的HTML Publisher Plugin 插件无法展示ant生成的JunitReport报告

    最近在做基于jenkins ant  junit 的测试持续集成,单独ant junit生成的junitreport报告打开正常,使用Jenkins的HTML Publisher Plugin 插件无 ...

  3. 常用 Gulp 插件汇总 —— 基于 Gulp 的前端集成解决方案(三)

    前两篇文章讨论了 Gulp 的安装部署及基本概念,借助于 Gulp 强大的 插件生态 可以完成很多常见的和不常见的任务.本文主要汇总常用的 Gulp 插件及其基本使用,需要读者对 Gulp 有一个基本 ...

  4. solr服务中集成IKAnalyzer中文分词器、集成dataimportHandler插件

    昨天已经在Tomcat容器中成功的部署了solr全文检索引擎系统的服务:今天来分享一下solr服务在海量数据的网站中是如何实现数据的检索. 在solr服务中集成IKAnalyzer中文分词器的步骤: ...

  5. 使用Visual Studio SDK制作GLSL词法着色插件

    使用Visual Studio SDK制作GLSL词法着色插件 我们在Visual Studio上开发OpenGL ES项目时,避免不了写Shader.这时在vs里直接编辑shader就会显得很方便. ...

  6. 工欲善其事,必先利其器 之 VS2013全攻略(安装,技巧,快捷键,插件)!

    如有需要WPF工具的朋友可以移步 工欲善其事,必先利其器 之 WPF篇: 随着开发轨迹来看高效WPF开发的工具和技巧 之前一篇<c++的性能, c#的产能?!鱼和熊掌可以兼得,.NET NATI ...

  7. Jquery mobiscroll 移动设备(手机)wap日期时间选择插件以及滑动、滚动插件

    Jquery Mobiscroll是一个用于触摸设备(Android phones, iPhone, iPad, Galaxy Tab)的日期和时间选择器jQuery插件.以及各种滑动插件 可以让用户 ...

  8. 10个最好用的HTML/CSS 工具、插件和资料库

    大家在使用HTML/CSS开发项目的过程中,有使用过哪些工具,插件和库?下面介绍的10种HTML/CSS工具,插件和资料库,是国外程序员经常用到的. Firebug Lite FirebugLite ...

  9. 在Sublime Text 3上安装代码格式化插件CodeFormatter

    1.了解CodeFormatter插件 在Sublime Text 3中编写代码,为了能让我们的代码格式变得漂亮整洁,需要一个能自动格式代码的插件.这里发现CodeFormatter插件不错,它能支持 ...

随机推荐

  1. Android Studio签名打包应用

    转载请注明来源: http://blog.csdn.net/kjunchen/article/details/50812391 可直接看看以下的Android Studio中签名应用 Android要 ...

  2. MySql command line client 命令系列

    —————————————————————————————————————————————————————————— 一.启动与退出 1.进入MySQL:启动MySQL Command Line Cl ...

  3. Objective-C语言的 if ( self = [super init] )

    我们先假设如今自己创建了个类.我们起名叫MyObject,继承于NSObject. 继承知道吧,就是你这个子类(MyObject)假设什么都不写的话,和父类(NSObject)就是一模一样的. OC里 ...

  4. Guava ---- EventBus事件驱动模型

    在软件开发过程中, 难免有信息的共享或者对象间的协作. 怎样让对象间信息共享高效, 而且耦合性低. 这是一个难题. 而耦合性高将带来编码改动牵一发而动全身的连锁效应. Spring的风靡正是由于攻克了 ...

  5. [办公自动化]excel工作簿内的表无法删除,单击右键无删除键

    今天同事问,我自己的工作簿,没有设置保护,但是就是无法删除其中的工作表. 后来发现,她的excel工作簿打开的文件名后面显示[共享]. 原因找到了. 取消共享就可以了.

  6. Delphi中WebBrowser控件打开部分网站报"Invalid floating point operation”解决

    Delphi中WebBrowser控件打开部分网站报"Invalid floating point operation”解决 EmbeddedWBWebBrowserDelphi  最近用E ...

  7. ARM+llinux系统移植3G拨号上网收发短信(二)【转】

    本文转载自:http://blog.csdn.net/hanmengaidudu/article/details/17099749 一.发送text格式的短信 给联通发text格式的短信: ~ > ...

  8. spark groupByKey().mapValues

    >>> rdd = sc.parallelize([("bone", 231), ("bone", 21213), ("jack&q ...

  9. luogu 1080 国王游戏

    题目大意: 有一些数对,每个数对的得分为它之前所有数对的左侧数之乘积除以它的右侧数 求重新排列后数列中所有数对中最大得分尽可能小(第一个数对不参与排序,仍然为第一个) 思路: 非常简单,可以根据它对后 ...

  10. BZOJ_3171_[Tjoi2013]循环格_最小费用最大流

    BZOJ_3171_[Tjoi2013]循环格_最小费用最大流 Description 一个循环格就是一个矩阵,其中所有元素为箭头,指向相邻四个格子.每个元素有一个坐标(行,列),其中左上角元素坐标为 ...