C# datagrideview插件的使用

 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==0||txtPwd.Length==0)
 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[0].Rows.Count > 0)
20             {
21                 int errorCount = Convert.ToInt32(dSet.Tables[0].Rows[0][3]);
22                 int times = Convert.ToInt32(dSet.Tables[0].Rows[0][5]);
23                 if (errorCount >= 3 && times <= 15)
24                 {
25                     if (dSet.Tables[0].Rows[0][1].ToString() == txtUserName && dSet.Tables[0].Rows[0][2].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[0].Rows[0][0].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[0].Rows[0][0].ToString());
41                         cmd = new SqlCommand(updateSql,conn);
42                         cmd.ExecuteNonQuery();
43                         conn.Close();
44                     }
45                 }
46                 else
47                 {
48                     MessageBox.Show("请在"+(15-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[0];
 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 = 2;
 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, 0);
 88
 89             SqlCommand cmd = new SqlCommand(sql, conn);
 90             conn.Open();
 91             int result = cmd.ExecuteNonQuery();
 92             conn.Close();
 93             if (result > 0)
 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, 2);
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[0].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 = 0;
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
42         public static DataSet ExecuteQuery(string sql) {
43
44             SqlConnection conn = new SqlConnection(connString);
45             SqlCommand cmd = new SqlCommand(sql, conn);
46             SqlDataAdapter da = new SqlDataAdapter(cmd);
47             DataSet ds = new DataSet();
48             conn.Open();
49             da.Fill(ds);
50             conn.Close();
51
52             return ds;
53         }
54     }
55 }

(二)、调用类

 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
42             if (isExistsLoginName(loginName))
43             {
44                 MessageBox.Show("该用户名已经存在,请重新输入");
45                 this.txtLoginName.Text = string.Empty;
46                 this.txtPWD.Text = string.Empty;
47                 return;
48             }
49
50             string sql = @"insert  into LoginInfo (loginName,pwd,LoginErrorLastTime)
51             values(‘{0}‘,‘{1}‘,‘{2}‘)";
52             sql = string.Format(sql, loginName, pwd, DateTime.Now.ToString());
53
54             int row = SqlCommon.ExecuteSql(sql);
55             if (row > 0)
56             {
57                 MessageBox.Show("添加成功");
58             }
59             else
60             {
61                 MessageBox.Show("添加失败");
62             }
63
64         }
65
66
67         #region 判断 用户名是否存在
68
69         private bool isExistsLoginName(string loginName)
70         {
71             string sql = @"select * from LoginInfo where LoginName=‘{0}‘";
72             sql = string.Format(sql, loginName);
73             DataSet ds = SqlCommon.ExecuteQuery(sql);
74             return ds.Tables[0].Rows.Count > 0;
75         }
76         #endregion
77     }
78 }

(三)、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[0];
31
32         }
33     }
34 }
时间: 2024-12-09 17:43:04

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

使用 Chrome 浏览器插件 Web Scraper 10分钟轻松实现网页数据的爬取

本文标签: WebScraper Chrome浏览器插件 网页数据的爬取 使用Chrome 浏览器插件 Web Scraper 可以轻松实现网页数据的爬取,不写代码,鼠标操作,点哪爬哪,还不用考虑爬虫中的登陆.验证码.异步加载等复杂问题. Web Scraper插件 Web Scraper 官网中的简介: Web Scraper Extension (Free!)Using our extension you can create a plan (sitemap) how a web site

eclipse插件之Findbugs、Checkstyle、PMD安装及使用

一.什么是Findbugs.checkstyle.PMD Findbugs.checkstyle和PMD都可以作为插件插入eclipse,当然也有单独的工具可以实现他们的功能,比如Findbugs Tool就可以不必插入eclipse就可以使用. 三者的功能如下表: 工具 目的 检查项 FindBugs 检查.class 基于Bug Patterns概念,查找javabytecode(.class文件)中的潜在bug 主要检查bytecode中的bug patterns,如NullPoint空指

eclipse安装java ee插件方法步骤

1.本人以前使用的MyEclipse进行Javaweb开发,但是后来由于myeclipse实在太臃肿,经常在运行的过程中不流畅 (可能电脑内存也不是太高吧)   !所以坚决换用eclipse,但是问题来了,下载的eclipse因为是标准版所以也不支持Javaweb开发,所以要来配置一下开发环境,安装Javaee开发插件   ,以下为安装步骤 1.1 在Eclipse中菜单help选项中选择install new software选项 1.2 在work with 栏中输入 http://down

Myeclipse10 安装Aptana插件

安装步骤: 1.下载aptana3.2 Eclipse Plugin插件. 下载地址:http://update1.aptana.org/studio/3.2/024747/index.html 2.在java文件夹下新建文件夹pluginsNew,在里面新建aptana_update_024747文件夹(这个文件夹名根据自己下载的版本自己写),再在里面新建eclipse文件夹,解压出features与plugins文件夹,COPY到 D:\java\pluginsNew\aptana_upda

这是一款借助chrome 插件的微信机器人

1.chrome kit微信机器人简介(github:https://github.com/LinuxForYQH/chrome_kit) 借助chrome 插件 js注入来实现消息的发送 chrome devtool api的调用来监听https请求 打开微信登录界面,在扫码登录前必须先打开toolbar(F12 或者 鼠标右键检查),如上所说因为借助了chrome devtool api所以需要打开toolbar才能执行相关dev域的js. 2.相关开发原理介绍 https://develo

使用插件bootstrap-table实现表格记录的查询、分页、排序等处理

在业务系统开发中,对表格记录的查询.分页.排序等处理是非常常见的,在Web开发中,可以采用很多功能强大的插件来满足要求,且能极大的提高开发效率,本随笔介绍这个bootstrap-table是一款非常有名的开源表格插件,在很多项目中广泛的应用.Bootstrap-table插件提供了非常丰富的属性设置,可以实现查询.分页.排序.复选框.设置显示列.Card view视图.主从表显示.合并列.国际化处理等处理功能,而且该插件同时也提供了一些不错的扩展功能,如移动行.移动列位置等一些特殊的功能,插件可

页面滚动图片等元素动态加载插件jquery.scrollLoading.js

如果一个网页很长,那么该页面的加载时间也会相应的较长.而这里给大家介绍的这个jQuery插件scrollLoading的作用则是,对页面元素进行动态加载,通俗的说就是滚到哪就加载到哪,屏幕以下看不见的就不用加载了.这样还可以在一定程度上节省服务器资源.该插件作者的网页将该插件的功能和使用方法描述的非常详细,这里把最一般最普遍的使用情况给大家展现一下. 插件作者:http://www.zhangxinxu.com/ 首先我们需要加载jQuery库和本插件js文件. (jquery.scrollLo

怎样将「插件化」接入到项目之中?

本期移动开发精英社群讨论的主题是「插件化」,上网查了一下,发现一篇 CSDN 博主写的文章<Android 使用动态载入框架DL进行插件化开发>.此处引用原作者的话: 随着应用的不断迭代,应用的体积不断增大,项目越来越臃肿,冗余添加.项目新功能的加入,无法确定与用户匹配性,发生严重异常往往牵一发而动全身,仅仅能紧急公布补丁版本号,强制用户进行更新.结果频繁的更新.反而easy减少用户使用黏性,或者是公司业务的不断发展,同系的应用越来越多,传统方式须要通过用户量最大的主项目进行引导下载并安装.

Android插件实例——360 DroidPlugin具体解释

在中国找到钱不难,但你的一个点子不意味着是一个创业.你谈一个再好的想法,比方我今天谈一个创意说,新浪为什么不收购GOOGLE呢?这个创意非常好.新浪一收购GOOGLE.是不是新浪就变成老大了?你从哪儿弄来钱?怎么去整合GOOGLE呢: 之前写过有关于Android 插件方向的文章,解析了一下Android的插件原理与执行方式.非常多小伙伴都问我.为什么不把我制作的插件放到Github上,让大家共享一下. 我仅仅能说.大哥啊,这个插件是我在公司研发的时候制作的,商业机密.不能开源啊. 刚好.近期逛