对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 ...
随机推荐
- Winform DataGridView控件在业务逻辑上的简单使用
需要对文字列表进行处理,然后用到DataGridView控件来处理,记录一下.效果如下: 主要是想通过禁用和取消单元格选择来使图标单元格呈现出鼠标点击的效果.因为有个单元格选择的问题困扰着我. 是这样 ...
- 新建WebAPI项目时遇到的问题
1 处理程序“ExtensionlessUrlHandler-Integrated-4.0”在其模块列表中有一个错误模块“ManagedPipelineHandler” 以管理员运行下面的命令注册 ...
- 【BZOJ1052】 [HAOI2007]覆盖问题
BZOJ1052 [HAOI2007]覆盖问题 前言 小清新思维题. 最近肯定需要一些思维题挽救我这种碰到题目只会模板的菜鸡. 这题腾空出世? Solution 考虑一下我们二分答案怎么做? 首先转换 ...
- python--Websocket实现, 加密 sha1,base64
需要用到gevent-websocket包,这里我们用下图这个 一.websocket简单实现 ep1.py from geventwebsocket.handler import WebSocket ...
- MariaDB MyCat实现读写分离(15)
MariaDB数据库管理系统是MySQL的一个分支,主要由开源社区在维护,采用GPL授权许可MariaDB的目的是完全兼容MySQL,包括API和命令行,MySQL由于现在闭源了,而能轻松成为MySQ ...
- SQL 一对多联表查询最大值
有两个数据表City表和Price表,CIty表的结构如下: Price表的结构如下: 查询每个城市最大的销售价格,并以最大价格进行降序排列,选取前5条记录,SQL语句的代码如下: * from (s ...
- 浅谈 Web 缓存
在前端开发中,性能一直都是被大家所重视的一点,然而判断一个网站的性能最直观的就是看网页打开的速度.其中提高网页反应速度的一个方式就是使用缓存.一个优秀的缓存策略可以缩短网页请求资源的距离,减少延迟,并 ...
- FastDFS高可用集群架构配置搭建
一.基本模块及高可用架构 FastDFS 是余庆老师开发的一个开源的高性能分布式文件系统(DFS). 它的主要功能包括:文件存储,文件同步和文件访问,以及高容量和负载平衡. FastDFS 系统有 ...
- vue教程2-05 v-for循环 重复数据无法添加问题 加track-by='索引'
vue教程2-05 v-for循环 重复数据无法添加问题 加track-by='索引' 解决问题的代码示例: <!DOCTYPE html> <html lang="en ...
- Servlet各种路径、URL配置分析
大家都知道,Servlet有个配置: <servlet> <servlet-name>zolltyMVC</servlet-name> <servlet-cl ...