对datagridview进行增删改(B)
create DATABASE stu
ON
(
name='stu.mdf',
filename='F:\胡浴东\数据库\stu数据库\stu.mdf',
size=,
filegrowth=
)
log ON
(
name='stu_log.ldf',
filename='F:\胡浴东\数据库\stu数据库\stu_log.ldf',
size=,
filegrowth=
)
go USE [stu]
GO
/****** Object: Table [dbo].[classinfo] Script Date: 2015/5/11 10:14:54 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[classinfo](
[c_id] [int] IDENTITY(,) NOT NULL,
[c_dep_id] [int] NULL,
[c_name] [varchar]() NULL,
PRIMARY KEY CLUSTERED
(
[c_id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY] GO
SET ANSI_PADDING OFF
GO
/****** Object: Table [dbo].[department] Script Date: 2015/5/11 10:14:54 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[department](
[dep_id] [int] IDENTITY(,) NOT NULL,
[dep_name] [varchar]() NULL,
PRIMARY KEY CLUSTERED
(
[dep_id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY] GO
SET ANSI_PADDING OFF
GO
/****** Object: Table [dbo].[logins] Script Date: 2015/5/11 10:14:54 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[logins](
[l_no] [varchar]() NOT NULL,
[l_pwd] [varchar]() NOT NULL,
[l_question] [varchar]() NOT NULL,
[l_answer] [varchar]() NOT NULL,
PRIMARY KEY CLUSTERED
(
[l_no] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY] GO
SET ANSI_PADDING OFF
GO
/****** Object: Table [dbo].[member] Script Date: 2015/5/11 10:14:54 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[member](
[username] [varchar]() NOT NULL,
[pwd] [varchar]() NOT NULL,
[name] [varchar]() NULL,
[gender] [char]() NULL,
[age] [varchar]() NULL,
[enjoy] [varchar]() NULL,
PRIMARY KEY CLUSTERED
(
[username] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY] GO
SET ANSI_PADDING OFF
GO
/****** Object: Table [dbo].[result] Script Date: 2015/5/11 10:14:54 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[result](
[r_id] [int] IDENTITY(,) NOT NULL,
[r_sub_id] [int] NULL,
[r_stu_id] [int] NULL,
[r_result] [int] NULL,
PRIMARY KEY CLUSTERED
(
[r_id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY] GO
/****** Object: Table [dbo].[studentinfo] Script Date: 2015/5/11 10:14:54 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[studentinfo](
[stu_id] [int] IDENTITY(,) NOT NULL,
[stu_name] [varchar]() NOT NULL,
[stu_age] [int] NULL,
[stu_c_id] [int] NULL,
[stu_gender] [char]() NULL,
[stu_address] [varchar]() NULL,
PRIMARY KEY CLUSTERED
(
[stu_id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY] GO
SET ANSI_PADDING OFF
GO
/****** Object: Table [dbo].[subject] Script Date: 2015/5/11 10:14:54 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[subject](
[sub_id] [int] IDENTITY(,) NOT NULL,
[sub_name] [varchar]() NULL,
PRIMARY KEY CLUSTERED
(
[sub_id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY] GO
SET ANSI_PADDING OFF
GO
/****** Object: View [dbo].[v_cool] Script Date: 2015/5/11 10:14:54 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
create view [dbo].[v_cool]
as
select s.stu_id,d.dep_name,c.c_name,s.stu_name,
s.stu_age,s.stu_gender,s.stu_address
from studentinfo s,classinfo c,department d
where s.stu_c_id=c.c_id and c.c_dep_id=d.dep_id
GO
/****** Object: View [dbo].[v_ggg] Script Date: 2015/5/11 10:14:54 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
create view [dbo].[v_ggg]
as
select s.stu_id 学员编号,s.stu_name 学员姓名,
s.stu_age 年龄,c.c_name 班级,s.stu_gender 性别,
s.stu_address 住址
from studentinfo s,classinfo c
where s.stu_c_id=c.c_id
GO
/****** Object: View [dbo].[View_cc] Script Date: 2015/5/11 10:14:54 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE VIEW [dbo].[View_cc]
AS
SELECT dbo.classinfo.c_name, dbo.department.dep_name, dbo.studentinfo.stu_age, dbo.studentinfo.stu_name,
dbo.studentinfo.stu_gender, dbo.studentinfo.stu_address
FROM dbo.classinfo INNER JOIN
dbo.department ON dbo.classinfo.c_dep_id = dbo.department.dep_id INNER JOIN
dbo.studentinfo ON dbo.classinfo.c_id = dbo.studentinfo.stu_c_id GO
SET IDENTITY_INSERT [dbo].[classinfo] ON INSERT [dbo].[classinfo] ([c_id], [c_dep_id], [c_name]) VALUES (, , N'')
INSERT [dbo].[classinfo] ([c_id], [c_dep_id], [c_name]) VALUES (, , N'')
INSERT [dbo].[classinfo] ([c_id], [c_dep_id], [c_name]) VALUES (, , N'')
INSERT [dbo].[classinfo] ([c_id], [c_dep_id], [c_name]) VALUES (, , N'')
SET IDENTITY_INSERT [dbo].[classinfo] OFF
SET IDENTITY_INSERT [dbo].[department] ON INSERT [dbo].[department] ([dep_id], [dep_name]) VALUES (, N'环化系')
INSERT [dbo].[department] ([dep_id], [dep_name]) VALUES (, N'汽车系')
INSERT [dbo].[department] ([dep_id], [dep_name]) VALUES (, N'信息智能系')
SET IDENTITY_INSERT [dbo].[department] OFF
INSERT [dbo].[logins] ([l_no], [l_pwd], [l_question], [l_answer]) VALUES (N'cool', N'', N'', N'')
INSERT [dbo].[logins] ([l_no], [l_pwd], [l_question], [l_answer]) VALUES (N'jack', N'', N'', N'')
INSERT [dbo].[logins] ([l_no], [l_pwd], [l_question], [l_answer]) VALUES (N'徐凯伦', N'', N'', N'')
INSERT [dbo].[member] ([username], [pwd], [name], [gender], [age], [enjoy]) VALUES (N'tom', N'', N'ss', N'男', N'90后', N'美女 帅哥 ')
INSERT [dbo].[member] ([username], [pwd], [name], [gender], [age], [enjoy]) VALUES (N'tom1', N'', N'ss', N'男', N'90后', N'美女 帅哥 ')
SET IDENTITY_INSERT [dbo].[result] ON INSERT [dbo].[result] ([r_id], [r_sub_id], [r_stu_id], [r_result]) VALUES (, , , )
INSERT [dbo].[result] ([r_id], [r_sub_id], [r_stu_id], [r_result]) VALUES (, , , )
INSERT [dbo].[result] ([r_id], [r_sub_id], [r_stu_id], [r_result]) VALUES (, , , )
INSERT [dbo].[result] ([r_id], [r_sub_id], [r_stu_id], [r_result]) VALUES (, , , )
INSERT [dbo].[result] ([r_id], [r_sub_id], [r_stu_id], [r_result]) VALUES (, , , )
INSERT [dbo].[result] ([r_id], [r_sub_id], [r_stu_id], [r_result]) VALUES (, , , )
INSERT [dbo].[result] ([r_id], [r_sub_id], [r_stu_id], [r_result]) VALUES (, , , )
INSERT [dbo].[result] ([r_id], [r_sub_id], [r_stu_id], [r_result]) VALUES (, , , )
INSERT [dbo].[result] ([r_id], [r_sub_id], [r_stu_id], [r_result]) VALUES (, , , )
INSERT [dbo].[result] ([r_id], [r_sub_id], [r_stu_id], [r_result]) VALUES (, , , )
INSERT [dbo].[result] ([r_id], [r_sub_id], [r_stu_id], [r_result]) VALUES (, , , )
INSERT [dbo].[result] ([r_id], [r_sub_id], [r_stu_id], [r_result]) VALUES (, , , )
SET IDENTITY_INSERT [dbo].[result] OFF
SET IDENTITY_INSERT [dbo].[studentinfo] ON INSERT [dbo].[studentinfo] ([stu_id], [stu_name], [stu_age], [stu_c_id], [stu_gender], [stu_address]) VALUES (, N'浩然刘', , , N'男', N'湖北宜昌')
INSERT [dbo].[studentinfo] ([stu_id], [stu_name], [stu_age], [stu_c_id], [stu_gender], [stu_address]) VALUES (, N'柯梦', , , N'女', N'湖北十堰')
INSERT [dbo].[studentinfo] ([stu_id], [stu_name], [stu_age], [stu_c_id], [stu_gender], [stu_address]) VALUES (, N'林智健', , , N'男', N'湖北天门')
INSERT [dbo].[studentinfo] ([stu_id], [stu_name], [stu_age], [stu_c_id], [stu_gender], [stu_address]) VALUES (, N'刘畅', , , N'男', N'湖北十堰')
INSERT [dbo].[studentinfo] ([stu_id], [stu_name], [stu_age], [stu_c_id], [stu_gender], [stu_address]) VALUES (, N'齐白石', , , N'男', N'湖北襄樊')
INSERT [dbo].[studentinfo] ([stu_id], [stu_name], [stu_age], [stu_c_id], [stu_gender], [stu_address]) VALUES (, N'刘佳', , , N'男', N'湖北十堰')
INSERT [dbo].[studentinfo] ([stu_id], [stu_name], [stu_age], [stu_c_id], [stu_gender], [stu_address]) VALUES (, N'张馨文', , , N'男', N'甘肃')
SET IDENTITY_INSERT [dbo].[studentinfo] OFF
SET IDENTITY_INSERT [dbo].[subject] ON INSERT [dbo].[subject] ([sub_id], [sub_name]) VALUES (, N'C#')
INSERT [dbo].[subject] ([sub_id], [sub_name]) VALUES (, N'html')
INSERT [dbo].[subject] ([sub_id], [sub_name]) VALUES (, N'sqlserver')
INSERT [dbo].[subject] ([sub_id], [sub_name]) VALUES (, N'环境监测')
INSERT [dbo].[subject] ([sub_id], [sub_name]) VALUES (, N'汽车维修')
SET IDENTITY_INSERT [dbo].[subject] OFF
SET ANSI_PADDING ON GO
/****** Object: Index [UQ__classinf__29397C81D09B43E9] Script Date: 2015/5/11 10:14:54 ******/
ALTER TABLE [dbo].[classinfo] ADD UNIQUE NONCLUSTERED
(
[c_name] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, SORT_IN_TEMPDB = OFF, IGNORE_DUP_KEY = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
GO
SET ANSI_PADDING ON GO
/****** Object: Index [UQ__departme__7BE54950D8CB39D9] Script Date: 2015/5/11 10:14:54 ******/
ALTER TABLE [dbo].[department] ADD UNIQUE NONCLUSTERED
(
[dep_name] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, SORT_IN_TEMPDB = OFF, IGNORE_DUP_KEY = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
GO
SET ANSI_PADDING ON GO
/****** Object: Index [UQ__subject__99BD2209CE37D31B] Script Date: 2015/5/11 10:14:54 ******/
ALTER TABLE [dbo].[subject] ADD UNIQUE NONCLUSTERED
(
[sub_name] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, SORT_IN_TEMPDB = OFF, IGNORE_DUP_KEY = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
GO
ALTER TABLE [dbo].[studentinfo] ADD DEFAULT ('湖北十堰') FOR [stu_address]
GO
ALTER TABLE [dbo].[classinfo] WITH CHECK ADD FOREIGN KEY([c_dep_id])
REFERENCES [dbo].[department] ([dep_id])
GO
ALTER TABLE [dbo].[result] WITH CHECK ADD FOREIGN KEY([r_stu_id])
REFERENCES [dbo].[studentinfo] ([stu_id])
GO
ALTER TABLE [dbo].[result] WITH CHECK ADD FOREIGN KEY([r_sub_id])
REFERENCES [dbo].[subject] ([sub_id])
GO
ALTER TABLE [dbo].[studentinfo] WITH CHECK ADD FOREIGN KEY([stu_c_id])
REFERENCES [dbo].[classinfo] ([c_id])
GO
ALTER TABLE [dbo].[member] WITH CHECK ADD CHECK (([gender]='女' OR [gender]='男'))
GO
ALTER TABLE [dbo].[studentinfo] WITH CHECK ADD CHECK (([stu_age]>=() AND [stu_age]<=()))
GO
ALTER TABLE [dbo].[studentinfo] WITH CHECK ADD CHECK (([stu_gender]='女' OR [stu_gender]='男'))
GO
EXEC sys.sp_addextendedproperty @name=N'MS_DiagramPane1', @value=N'[0E232FF0-B466-11cf-A24F-00AA00A3EFFF, 1.00]
Begin DesignProperties =
Begin PaneConfigurations =
Begin PaneConfiguration =
NumPanes =
Configuration = "(H (1[11] 4[56] 2[16] 3) )"
End
Begin PaneConfiguration =
NumPanes =
Configuration = "(H (1 [50] 4 [25] 3))"
End
Begin PaneConfiguration =
NumPanes =
Configuration = "(H (1 [50] 2 [25] 3))"
End
Begin PaneConfiguration =
NumPanes =
Configuration = "(H (4 [30] 2 [40] 3))"
End
Begin PaneConfiguration =
NumPanes =
Configuration = "(H (1 [56] 3))"
End
Begin PaneConfiguration =
NumPanes =
Configuration = "(H (2 [66] 3))"
End
Begin PaneConfiguration =
NumPanes =
Configuration = "(H (4 [50] 3))"
End
Begin PaneConfiguration =
NumPanes =
Configuration = "(V (3))"
End
Begin PaneConfiguration =
NumPanes =
Configuration = "(H (1[56] 4[18] 2) )"
End
Begin PaneConfiguration =
NumPanes =
Configuration = "(H (1 [75] 4))"
End
Begin PaneConfiguration =
NumPanes =
Configuration = "(H (1[66] 2) )"
End
Begin PaneConfiguration =
NumPanes =
Configuration = "(H (4 [60] 2))"
End
Begin PaneConfiguration =
NumPanes =
Configuration = "(H (1) )"
End
Begin PaneConfiguration =
NumPanes =
Configuration = "(V (4))"
End
Begin PaneConfiguration =
NumPanes =
Configuration = "(V (2))"
End
ActivePaneConfig =
End
Begin DiagramPane =
Begin Origin =
Top = -
Left =
End
Begin Tables =
Begin Table = "classinfo"
Begin Extent =
Top =
Left =
Bottom =
Right =
End
DisplayFlags =
TopColumn =
End
Begin Table = "department"
Begin Extent =
Top =
Left =
Bottom =
Right =
End
DisplayFlags =
TopColumn =
End
Begin Table = "studentinfo"
Begin Extent =
Top =
Left =
Bottom =
Right =
End
DisplayFlags =
TopColumn =
End
End
End
Begin SQLPane =
End
Begin DataPane =
Begin ParameterDefaults = ""
End
End
Begin CriteriaPane =
Begin ColumnWidths =
Column =
Alias =
Table =
Output =
Append =
NewValue =
SortType =
SortOrder =
GroupBy =
Filter =
Or =
Or =
Or =
End
End
End
' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'VIEW',@level1name=N'View_cc'
GO
EXEC sys.sp_addextendedproperty @name=N'MS_DiagramPaneCount', @value= , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'VIEW',@level1name=N'View_cc'
GO
数据库
在找工作的过程中,datagridview大多是这样使用的,运行效果如下:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data;
using System.Data.SqlClient; namespace Phone
{
class DBHelper
{
public static readonly string Constring = "Data Source=.;Initial Catalog=Phone;Integrated Security=True";//连接数据库
public static SqlConnection con = new SqlConnection(Constring); //打开数据库
public static void ConOpen()
{
if (con.State==ConnectionState.Closed)
{
con.Open();
}
} //关闭数据库
public static void ConClose()
{
if (con.State != ConnectionState.Closed)
{
con.Close();
}
} //查询有三种方法
//方法一
public static DataTable getTable(string sql)
{
ConOpen();
SqlDataAdapter apt = new SqlDataAdapter(sql, con);
DataTable dt = new DataTable();
apt.Fill(dt);
return dt;
} //方法二
public static SqlDataReader getReader(string sql)
{
ConOpen();
SqlCommand cmd = new SqlCommand(sql,con);
SqlDataReader reader = cmd.ExecuteReader();
return reader;
} //方法三
public static Object Scalar(string sql)
{
ConOpen();
SqlCommand cmd = new SqlCommand(sql,con);
object obj = cmd.ExecuteScalar();
ConClose();
return obj;
} //增加删除修改
public static int ExecuteNonQuery(string sql)
{
int rult=;
try
{
ConOpen();
SqlCommand cmd = new SqlCommand(sql, con);
rult = cmd.ExecuteNonQuery();
}
catch (Exception)
{
;
}
finally
{
ConClose();
}
return rult;
}
}
}
DBHelper
- 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 stu
- {
- public partial class stu : Form
- {
- public stu()
- {
- InitializeComponent();
- }
- //返回
- private void btn_esc_Click(object sender, EventArgs e)
- {
- Bind(string.Empty,string.Empty);
- }
- //绑定、查找
- public void Bind(string cbo, string txt)
- {
- this.cbo_search.SelectedIndex = 0;
- string sql = string.Format("select stu_id 编号, stu_name 姓名,c_name 班级, stu_age 年龄, stu_gender 性别, stu_address 地址 from studentinfo,classinfo where stu_c_id=c_id ");
- if (!txt.Equals(""))
- {
- switch (cbo)
- {
- case "姓名":
- sql += string.Format(" and stu_name like '%{0}%'", txt);
- break;
- case "班级":
- sql += string.Format(" and c_name like '%{0}%'", txt);
- break;
- case "年龄":
- sql += string.Format(" and stu_age like '%{0}%'", txt);
- break;
- case "性别":
- sql += string.Format(" and stu_gender like '%{0}%'", txt);
- break;
- case "地址":
- sql += string.Format(" and stu_address like '%{0}%'", txt);
- break;
- }
- }
- this.dgv_stu.DataSource = DBHelper.GetTable(sql);
- }
- //绑定班级
- public void Binding()
- {
- string sql1 = string.Format("select c_id,c_name from classinfo");
- this.cbo_class.DisplayMember = "c_name";
- this.cbo_class.ValueMember = "c_id";
- this.cbo_class.DataSource = DBHelper.GetTable(sql1);
- }
- //窗体加载事件
- private void stu_Load(object sender, EventArgs e)
- {
- Bind(string.Empty,string.Empty);
- Binding();
- }
- //查找
- private void btn_search_Click(object sender, EventArgs e)
- {
- string cbo=this.cbo_search.Text;
- string txt=this.txt_search.Text.Trim();
- Bind(cbo,txt);
- }
- //非空验证
- public bool Input()
- {
- foreach (System.Windows.Forms.Control cc in groupBox3.Controls)
- {
- if (cc is TextBox)
- {
- if (cc.Text.Equals(""))
- {
- MessageBox.Show("文本框不能为空!");
- return false;
- }
- }
- }
- return true;
- }
- //清空
- public void Clear()
- {
- foreach (System.Windows.Forms.Control cc in groupBox3.Controls)
- {
- if (cc is TextBox)
- {
- if (!cc.Text.Equals(""))
- {
- cc.Text = string.Empty;
- }
- }
- }
- this.cbo_class.SelectedValue= true;
- this.rdo_man.Checked = true;
- }
- //数据申明
- public static int id;
- public static int c_id;
- public static string name = string.Empty;
- public static string c_name = string.Empty;
- public static int age =0;
- public static string gender = string.Empty;
- public static string address = string.Empty;
- //获取行索引
- public void ShuJu()
- {
- name = this.txt_name.Text.Trim();
- c_id =Convert.ToInt32(this.cbo_class.SelectedValue.ToString());
- try
- {
- age=Convert.ToInt32(this.txt_age.Text.Trim());
- if (age<0)
- {
- MessageBox.Show("请输入正确的年龄!");
- return;
- }
- }
- catch (Exception)
- {
- MessageBox.Show("请输入正确的年龄!");
- return;
- }
- gender=this.rdo_man.Checked==true?"男":"女";
- address = this.txt_address.Text.Trim();
- }
- //添加
- private void btn_add_Click(object sender, EventArgs e)
- {
- if (Input())
- {
- ShuJu();
- string sql2 = string.Format("insert into studentinfo values('{0}','{1}','{2}','{3}','{4}')",name,age,c_id,gender,address);
- int rult1 = DBHelper.ExecuteNonQuery(sql2);
- if (rult1>0)
- {
- MessageBox.Show("添加成功!");
- Bind(string.Empty,string.Empty);
- Clear();
- return;
- }
- else
- {
- MessageBox.Show("添加失败!");
- return;
- }
- }
- }
- //修改
- private void btn_update_Click(object sender, EventArgs e)
- {
- if (Input())
- {
- ShuJu();
- string sql3 = string.Format("update studentinfo set stu_name='{0}',stu_age='{1}',stu_c_id='{2}',stu_gender='{3}',stu_address='{4}' where stu_id='{5}'",name,age,c_id,gender,address,id);
- int rult2 = DBHelper.ExecuteNonQuery(sql3);
- if (rult2>-0)
- {
- MessageBox.Show("修改成功!");
- Bind(string.Empty,string.Empty);
- Clear();
- return;
- }
- else
- {
- MessageBox.Show("修改失败!");
- return;
- }
- }
- }
- //单击任意单元格事件
- private void dgv_stu_CellClick(object sender, DataGridViewCellEventArgs e)
- {
- int rowIndex=this.dgv_stu.CurrentCell.RowIndex;
- if (rowIndex > -1)
- {
- if (this.dgv_stu[0,rowIndex].Value.ToString()!=string.Empty)
- {
- id =Convert.ToInt32(this.dgv_stu[0, rowIndex].Value.ToString());
- this.txt_name.Text = this.dgv_stu[1, rowIndex].Value.ToString();
- this.cbo_class.Text = this.dgv_stu[2, rowIndex].Value.ToString();
- this.txt_age.Text = this.dgv_stu[3, rowIndex].Value.ToString();
- if (this.dgv_stu[4, rowIndex].Value.ToString() == "男")
- {
- this.rdo_man.Checked = true;
- }
- else
- {
- this.rdo_woman.Checked = true;
- }
- this.txt_address.Text = this.dgv_stu[5, rowIndex].Value.ToString();
- }
- else
- {
- MessageBox.Show("请选择有数据的行!");
- return;
- }
- }
- }
- //删除
- private void 删除ToolStripMenuItem_Click(object sender, EventArgs e)
- {
- if (Input())
- {
- int rowIndex = this.dgv_stu.CurrentCell.RowIndex;
- if (rowIndex>-1)
- {
- if (this.dgv_stu[0,rowIndex].Value.ToString()!=string.Empty)
- {
- string sql4 = string.Format("delete studentinfo where stu_id='{0}'",id);
- int rult3 = DBHelper.ExecuteNonQuery(sql4);
- if (rult3>-1)
- {
- MessageBox.Show("删除成功!");
- Bind(string.Empty, string.Empty);
- Clear();
- return;
- }
- else
- {
- MessageBox.Show("删除失败!");
- return;
- }
- }
- else
- {
- MessageBox.Show("请选择有数据的行!");
- return;
- }
- }
- }
- }
- }
- }
对datagridview进行增删改(B)的更多相关文章
- 使用DataGridView进行增删改查,并同步到数据库
DataGridView控件具有极高的可配置性和可扩展性.它提供有大量的属性.方法和事件,能够用来对该控件的外观和行为进行自己定义.以下通过一个小样例来展示DataGridView进行增删改查,并同步 ...
- 浅谈dataGridView使用,以及画面布局使用属性,对datagridview进行增删改查操作,以及委托使用技巧
通过几天的努力后,对datagridview使用作一些简要的介绍,该实例主要运用与通过对datagridview操作.对数据进行增删改查操作时,进行逻辑判断执行相关操作.简单的使用委托功能,实 ...
- Winform开发之DataGridView的增删改
DataGridView是一个非常强大的控件,用法很多.这里介绍一个简单的增删改例子. 贴效果图 右侧输入学生信息点击新增,将数据增加到数据库,并且加载到datagridview中,点击选择某条数据修 ...
- SqlDataAdapter 对datagridview进行增删改(A)
这种方法主要是双击datagridview单元格,直接进行添加,修改,删除,在实际开发中并不太常用,另一种方法下一次在具体陈述. using System; using System.Collecti ...
- winform窗体(六)——DataGridView控件及通过此控件中实现增删改查
DataGridView:显示数据表,通过此控件中可以实现连接数据库,实现数据的增删改查 一.后台数据绑定: List<xxx> list = new List<xxx> ...
- Winform(DataGridView)控件及通过此控件中实现增删改查
DataGridView:显示数据表,通过此控件中可以实现连接数据库,实现数据的增删改查 一.后台数据绑定: List<xxx> list = new List<xxx> ...
- WinForm DataGridView增删改查
DataGridView连接数据库对表进行增删改查 一.绑定数据源 //做一个变量控制页面刷新 ; public Form1() { InitializeComponent(); } private ...
- datagridview数据绑定操作数据库实现增删改查
DataSet数据集,数据缓存在客户端内存中,支持断开式连接.DataGridView控件绑定DataSet时,它自动的改变的DS的行的状态,而且在做增删改查的时候,可以借助SqlCommandBui ...
- c#操作数据库的增删改查语句及DataGridView简单使用
下面是要用户名和密码连接数据库的操作: 一.定义连接字符串,用来链接SQL Server string str_con = "server=.(服务器名称一般为 . );database=W ...
随机推荐
- title
事实上 @import url(http://i.cnblogs.com/Load.ashx?type=style&file=SyntaxHighlighter.css);@import ur ...
- RNN以及LSTM的介绍和公式梳理
前言 好久没用正儿八经地写博客了,csdn居然也有了markdown的编辑器了,最近花了不少时间看RNN以及LSTM的论文,在组内『夜校』分享过了,再在这里总结一下发出来吧,按照我讲解的思路,理解RN ...
- LogisticRegression in MLLib (PySpark + numpy+matplotlib可视化)
参考'LogisticRegression in MLLib' (http://www.cnblogs.com/luweiseu/p/7809521.html) 通过pySpark MLlib训练lo ...
- 【转】基于Token的WEB后台认证机制
原谅地址:http://www.cnblogs.com/xiekeli/p/5607107.html 几种常用的认证机制 HTTP Basic Auth HTTP Basic Auth简单点说明就是每 ...
- 《Python自动化运维之路》 系统基础信息模块(一)
系统性能收集模块Psutil 目录: 系统性能信息模块psutil 系统性能信息模块psutil psutil能够轻松实现获取系统运行的进程和系统利用率包括(CPU,内存,磁盘 和网络)等.主要用于系 ...
- [SDOI2011]消耗战(虚树+树形动规)
虚树dp 虚树的主要思想: 不遍历没用的的节点以及没用的子树,从而使复杂度降低到\(\sum\limits k\)(k为询问的节点的总数). 所以怎么办: 只把询问节点和其LCA放入询问的数组中. 1 ...
- Windows 网卡超过序列
以前连接过别的网络,现在重新装网已经排到"网络连接9"了,连以前起过的wifi名称(ssid)都变成"*** 3"了,怎么把以前的清空呢? 这是解决办法: wi ...
- js 标准二维数组变一维数组的方法
问题:[[0, 1], [2, 3], [4, 5]] -> [0, 1, 2, 3, 4, 5]? 方法一 利用es5的arr.reduce(callback[, initialValue]) ...
- git小乌龟工具TortoiseGit记住你的账号密码
在使用TortoiseGit的过程中,发下每次push或者pull都要重复输入账号密码,非常麻烦 怎么设置记住密码 在[系统盘]:\Users[你的用户名](比如C:\User\Administrat ...
- 从C#到TypeScript - 类型
总目录 从C#到TypeScript - 类型 从C#到TypeScript - 高级类型 从C#到TypeScript - 变量 从C#到TypeScript - 接口 从C#到TypeScript ...