private void btnLogin_Click(object sender, EventArgs e)
{
string txtUserName = this.txtUserName.Text.Trim();
string txtPwd = this.txtPwd.Text.Trim();
if (txtUserName==null||txtPwd==null||txtUserName.Length==||txtPwd.Length==)
{
MessageBox.Show("您输入的内容为空,请重新输入!");
}
string connString = "server=.;database=StudentMISDB;uid=sa;pwd=123456";
SqlConnection conn = new SqlConnection(connString);
conn.Open();
string sql = @"select l.*,DATEDIFF(MI,LoginErrorLastTime,GETDATE()) as 间隔 from login as l where loginname='{0}'";
sql = string.Format(sql, txtUserName);
SqlCommand cmd = new SqlCommand(sql,conn);
SqlDataAdapter adapter = new SqlDataAdapter(cmd);
DataSet dSet = new DataSet();
adapter.Fill(dSet);
conn.Close();
if (dSet.Tables[].Rows.Count > )
{
int errorCount = Convert.ToInt32(dSet.Tables[].Rows[][]);
int times = Convert.ToInt32(dSet.Tables[].Rows[][]);
if (errorCount >= && times <= )
{
if (dSet.Tables[].Rows[][].ToString() == txtUserName && dSet.Tables[].Rows[][].ToString()==txtPwd)
{
MessageBox.Show("登陆成功");
conn.Open();
string uptateSql = @"update login set loginerrorcount=0 where id='{0}'";
uptateSql = string.Format(uptateSql,dSet.Tables[].Rows[][].ToString());
cmd = new SqlCommand(uptateSql,conn);
cmd.ExecuteNonQuery();
conn.Close();
}
else
{
MessageBox.Show("登录名或者密码错误!");
conn.Open();
string updateSql = @"update login set loginerrorcount=loginerrorcount+1 ,loginerrorlasttime=getdate() where id='{0}'";
updateSql = string.Format(updateSql,dSet.Tables[].Rows[][].ToString());
cmd = new SqlCommand(updateSql,conn);
cmd.ExecuteNonQuery();
conn.Close();
}
}
else
{
MessageBox.Show("请在"+(-times)+"分钟后登录!");
}
}
else
{
MessageBox.Show("用户不存在!");
}
}
}
 using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient; namespace ADO.NET8._30
{
public partial class DataGridView : Form
{
public DataGridView()
{
InitializeComponent();
} private void DataGridView_Load(object sender, EventArgs e)
{
//加载学生选课信息
loadStudntData();
//加载课程信息(没有选的课程)
LoadCourse();
}
/// <summary>
/// 自定义一个方法
/// </summary>
private void LoadCourse()
{
//1.创建数据库连接符
string connString = "server=.;database=StudentMISDB;uid=sa;pwd=123456";
//2.链接数据库
SqlConnection conn = new SqlConnection(connString);
//3.添加数据库要执行的语句,通过ID查找没有选择到的课程
string sql = @"select * from Course where CourseId not in(select distinct sc.CourseId from [dbo].[Students] as s
join Score as sc on s.StudentId=sc.StudentId
join Course as c on sc.CourseId = c.CourseId
where s.StudentId='2')";
//4.创建命令
SqlCommand cmd = new SqlCommand(sql, conn);
//5.断开式连接查询
SqlDataAdapter da = new SqlDataAdapter(cmd);
//6.创建数据缓冲区(数据集)
DataSet ds = new DataSet();
conn.Open();
//7.填充数据集
da.Fill(ds);
conn.Close();
//8.绑定数据源
this.cmbCourseName.DataSource = ds.Tables[]; //9.设置combobox控件中要显示的列
//this.cmboxCourse.DisplayMember = "列名";
this.cmbCourseName.DisplayMember = "Name";
//10.DisplayMember绑定需要显示的数据表字段,而ValueMember绑定需要获取选择的项的值;直接可见的是此item的 DisplayMember 对应内容,而此 item的值是ValueMember 的对应内容。
this.cmbCourseName.ValueMember = "CourseId";
}
/// <summary>
/// 获取选中的值
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void cmbCourseName_SelectedIndexChanged(object sender, EventArgs e)
{
string courseID = this.cmbCourseName.SelectedValue.ToString();
///string courseName = this.cmboxCourse.SelectedText;
}
/// <summary>
/// 保存 选课后,
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnSave_Click(object sender, EventArgs e)
{
//获取到在combobox控件中已经选择的值
string courseID = this.cmbCourseName.SelectedValue.ToString();
//string courseName = this.cmboxCourse.SelectedText;
int studentId = ;
//1.
string connString = "server=.;database=StudentMISDB;uid=sa;pwd=123456";
SqlConnection conn = new SqlConnection(connString); string sql = "insert into Score values('{0}','{1}','{2}')";
sql = string.Format(sql, studentId, courseID, ); SqlCommand cmd = new SqlCommand(sql, conn);
conn.Open();
int result = cmd.ExecuteNonQuery();
conn.Close();
if (result > )
{
MessageBox.Show("保存成功"); loadStudntData();
LoadCourse();
}
else
{
MessageBox.Show("保存失败");
}
}
/// <summary>
/// 加载学生选课信息
/// </summary>
private void loadStudntData()
{
string connString = "server=.;database=StudentMISDB;uid=sa;pwd=123456";
SqlConnection conn = new SqlConnection(connString); string sql = @"select s.Studentid,c.courseId, s.Name as 姓名,c.Name as 课程名, sc.Score as 成绩 from [dbo].[Students] as s
join Score as sc on s.StudentId=sc.StudentId
join Course as c on sc.CourseId = c.CourseId
where s.StudentId='{0}'";
sql = string.Format(sql, ); SqlCommand cmd = new SqlCommand(sql, conn);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
conn.Open();
da.Fill(ds);
conn.Close(); this.dataGridView1.DataSource = ds;
this.dataGridView1.DataMember = ds.Tables[].TableName;
}
}
}

二、(1)封装的类:

 using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data; namespace Demo2
{
public static class SqlCommon
{ /// <summary>
/// 连接字符串
/// </summary>
public static string connString = "server=.;database=StudentMISDB;uid=sa;pwd=123456"; /// <summary>
/// 执行增删改操作
/// </summary>
/// <param name="sql">sql语句 参数传入</param>
/// <returns></returns>
public static int ExecuteSql(string sql)
{
int result = ;
//创建连接对象new SqlConnection( 连接字符串)
SqlConnection conn = new SqlConnection(connString);
//创建命令对象 new SqlCommand(sql语句, conn)
SqlCommand cmd = new SqlCommand(sql, conn);
// 打开数据连接
conn.Open();
// 执行 sql 命令,返回受影响的行数
result = cmd.ExecuteNonQuery();
// 关闭数据连接
conn.Close();
// 把执行结果【受影响的行数】,返回给调用者
return result;
} public static DataSet ExecuteQuery(string sql) { SqlConnection conn = new SqlConnection(connString);
SqlCommand cmd = new SqlCommand(sql, conn);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
conn.Open();
da.Fill(ds);
conn.Close(); return ds;
}
}
}

(二)、调用类

 using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms; using System.Data.SqlClient; namespace Demo2
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
} private void btnAdd_Click(object sender, EventArgs e)
{
/// 定义变量接收 用户输入的值
string loginName = this.txtLoginName.Text.Trim();
string pwd = this.txtPWD.Text.Trim(); ///判断用户是否有输入值
if (loginName == "" || loginName == null)
{
MessageBox.Show("请输入用户名");
return;
}
//string.IsNullOrEmpty(字符串) ==>判断字符串是否为“空字符”或为 null
if (string.IsNullOrEmpty(pwd))
{
MessageBox.Show("请输入密码");
return;
} if (isExistsLoginName(loginName))
{
MessageBox.Show("该用户名已经存在,请重新输入");
this.txtLoginName.Text = string.Empty;
this.txtPWD.Text = string.Empty;
return;
} string sql = @"insert into LoginInfo (loginName,pwd,LoginErrorLastTime)
values('{0}','{1}','{2}')";
sql = string.Format(sql, loginName, pwd, DateTime.Now.ToString()); int row = SqlCommon.ExecuteSql(sql);
if (row > )
{
MessageBox.Show("添加成功");
}
else
{
MessageBox.Show("添加失败");
} } #region 判断 用户名是否存在 private bool isExistsLoginName(string loginName)
{
string sql = @"select * from LoginInfo where LoginName='{0}'";
sql = string.Format(sql, loginName);
DataSet ds = SqlCommon.ExecuteQuery(sql);
return ds.Tables[].Rows.Count > ;
}
#endregion
}
}

(三)、datagrideview插件

 using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms; namespace Demo2
{
public partial class FrmScore : Form
{
public FrmScore()
{
InitializeComponent();
} private void btnSelect_Click(object sender, EventArgs e)
{
string name = this.txtCourse.Text.Trim(); string sql = @"select ScoreId,Name,Score from Course as c
join Score as sc on c.CourseId = sc.CourseId
where sc.StudentId='1' and c.Name like '%{0}%'";
sql = string.Format(sql,name);
DataSet ds = SqlCommon.ExecuteQuery(sql); this.dataGridView1.DataSource = ds.Tables[]; }
}
}

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. 条款十七: 在operator=中检查给自己赋值的情况

    在赋值运算符中要特别注意可能出现别名的情况,其理由基于两点.其中之一是效率.如果可以在赋值运算符函数体的首部检测到是给自己赋值,就可以立即返回,从而可以节省大量的工作,否则必须去实现整个赋值操作. 另 ...

  2. X-pack-6.2.4破解

    1.前言: X-pack是elasticsearch的一个扩展包,将安全,警告,监视,图形和报告功能捆绑在一个易于安装的软件包中,虽然x-pack被设计为一个无缝的工作,但是你可以轻松的启用或者关闭一 ...

  3. 移动硬盘/U盘上装Windows 7旗舰版(VHD版)

    真正的移动版WIN7,在移动硬盘/U盘上运行的WIN7 工具准备 - 联想Y450本本,已安装Windows 7旗舰版(或者WINPE3.0版),用来给移动WIN7做引导 -Win7.vhd,15G, ...

  4. struts1与struts2的差别

     Struts 2是Struts的下一代产品,是在 struts 1和WebWork的技术基础上进行了合并的全新的Struts 2框架.其全新的Struts 2的体系结构与Struts 1的体系结 ...

  5. Cocos2dx使用ios内支付IAP具体流程-白白

    今天总结了一下cocos2d-x使用ios内支付iap的具体流程,封装好了调用接口,代码与具体说明在此 http://download.csdn.net/detail/u010229677/81566 ...

  6. Node.js创建自签名的HTTPS服务器

    https://cnodejs.org/topic/54745ac22804a0997d38b32d  用Node.js创建自签名的HTTPS服务器  发布于 4 年前  作者 eeandrew  6 ...

  7. HDU 5762Teacher Bo

    Teacher Bo Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)Tota ...

  8. 雅礼集训 2017 Day1

    T1:loj 6029 市场 题目大意: 维护一个数据结构支持区间加 区间除法 区间求最小值 区间求和 思路: 用线段树维护区间加 区间求最小值 区间和 对于区间除法 注意到除数d很大而加法的w很小 ...

  9. U74201 旅行计划 树上找链长度

    题目背景 珂朵莉放假了,她打算去唐山旅行. 题目描述 我们简单地把唐山的共 nn 个景点看成是一棵树,有 n-1n−1 条边将它们连接起来,每个景点有一个游览指数 v_ivi​.珂朵莉的假期时间不长, ...

  10. redirect和forward 的区别

    1.从地址栏显示来说 forward 是服务器请求资源,服务器直接访问目标地址url,把那个url的响应内容读取过来,然后把这些内容再发给浏览器,浏览器根本不知道服务器发送的内容从哪里来的,所以他的地 ...