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

  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. namespace stu
  11. {
  12. public partial class stu : Form
  13. {
  14. public stu()
  15. {
  16. InitializeComponent();
  17. }
  18. //返回
  19. private void btn_esc_Click(object sender, EventArgs e)
  20. {
  21. Bind(string.Empty,string.Empty);
  22. }
  23. //绑定、查找
  24. public void Bind(string cbo, string txt)
  25. {
  26. this.cbo_search.SelectedIndex = 0;
  27. 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 ");
  28. if (!txt.Equals(""))
  29. {
  30. switch (cbo)
  31. {
  32. case "姓名":
  33. sql += string.Format(" and stu_name like '%{0}%'", txt);
  34. break;
  35. case "班级":
  36. sql += string.Format(" and c_name like '%{0}%'", txt);
  37. break;
  38. case "年龄":
  39. sql += string.Format(" and stu_age like '%{0}%'", txt);
  40. break;
  41. case "性别":
  42. sql += string.Format(" and stu_gender like '%{0}%'", txt);
  43. break;
  44. case "地址":
  45. sql += string.Format(" and stu_address like '%{0}%'", txt);
  46. break;
  47. }
  48. }
  49. this.dgv_stu.DataSource = DBHelper.GetTable(sql);
  50. }
  51. //绑定班级
  52. public void Binding()
  53. {
  54. string sql1 = string.Format("select c_id,c_name from classinfo");
  55. this.cbo_class.DisplayMember = "c_name";
  56. this.cbo_class.ValueMember = "c_id";
  57. this.cbo_class.DataSource = DBHelper.GetTable(sql1);
  58. }
  59. //窗体加载事件
  60. private void stu_Load(object sender, EventArgs e)
  61. {
  62. Bind(string.Empty,string.Empty);
  63. Binding();
  64. }
  65. //查找
  66. private void btn_search_Click(object sender, EventArgs e)
  67. {
  68. string cbo=this.cbo_search.Text;
  69. string txt=this.txt_search.Text.Trim();
  70. Bind(cbo,txt);
  71. }
  72. //非空验证
  73. public bool Input()
  74. {
  75. foreach (System.Windows.Forms.Control cc in groupBox3.Controls)
  76. {
  77. if (cc is TextBox)
  78. {
  79. if (cc.Text.Equals(""))
  80. {
  81. MessageBox.Show("文本框不能为空!");
  82. return false;
  83. }
  84. }
  85. }
  86. return true;
  87. }
  88. //清空
  89. public void Clear()
  90. {
  91. foreach (System.Windows.Forms.Control cc in groupBox3.Controls)
  92. {
  93. if (cc is TextBox)
  94. {
  95. if (!cc.Text.Equals(""))
  96. {
  97. cc.Text = string.Empty;
  98. }
  99. }
  100. }
  101. this.cbo_class.SelectedValue= true;
  102. this.rdo_man.Checked = true;
  103. }
  104. //数据申明
  105. public static int id;
  106. public static int c_id;
  107. public static string name = string.Empty;
  108. public static string c_name = string.Empty;
  109. public static int age =0;
  110. public static string gender = string.Empty;
  111. public static string address = string.Empty;
  112. //获取行索引
  113. public void ShuJu()
  114. {
  115. name = this.txt_name.Text.Trim();
  116. c_id =Convert.ToInt32(this.cbo_class.SelectedValue.ToString());
  117. try
  118. {
  119. age=Convert.ToInt32(this.txt_age.Text.Trim());
  120. if (age<0)
  121. {
  122. MessageBox.Show("请输入正确的年龄!");
  123. return;
  124. }
  125. }
  126. catch (Exception)
  127. {
  128. MessageBox.Show("请输入正确的年龄!");
  129. return;
  130. }
  131. gender=this.rdo_man.Checked==true?"男":"女";
  132. address = this.txt_address.Text.Trim();
  133. }
  134. //添加
  135. private void btn_add_Click(object sender, EventArgs e)
  136. {
  137. if (Input())
  138. {
  139. ShuJu();
  140. string sql2 = string.Format("insert into studentinfo values('{0}','{1}','{2}','{3}','{4}')",name,age,c_id,gender,address);
  141. int rult1 = DBHelper.ExecuteNonQuery(sql2);
  142. if (rult1>0)
  143. {
  144. MessageBox.Show("添加成功!");
  145. Bind(string.Empty,string.Empty);
  146. Clear();
  147. return;
  148. }
  149. else
  150. {
  151. MessageBox.Show("添加失败!");
  152. return;
  153. }
  154. }
  155. }
  156. //修改
  157. private void btn_update_Click(object sender, EventArgs e)
  158. {
  159. if (Input())
  160. {
  161. ShuJu();
  162. 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);
  163. int rult2 = DBHelper.ExecuteNonQuery(sql3);
  164. if (rult2>-0)
  165. {
  166. MessageBox.Show("修改成功!");
  167. Bind(string.Empty,string.Empty);
  168. Clear();
  169. return;
  170. }
  171. else
  172. {
  173. MessageBox.Show("修改失败!");
  174. return;
  175. }
  176. }
  177. }
  178. //单击任意单元格事件
  179. private void dgv_stu_CellClick(object sender, DataGridViewCellEventArgs e)
  180. {
  181. int rowIndex=this.dgv_stu.CurrentCell.RowIndex;
  182. if (rowIndex > -1)
  183. {
  184. if (this.dgv_stu[0,rowIndex].Value.ToString()!=string.Empty)
  185. {
  186. id =Convert.ToInt32(this.dgv_stu[0, rowIndex].Value.ToString());
  187. this.txt_name.Text = this.dgv_stu[1, rowIndex].Value.ToString();
  188. this.cbo_class.Text = this.dgv_stu[2, rowIndex].Value.ToString();
  189. this.txt_age.Text = this.dgv_stu[3, rowIndex].Value.ToString();
  190. if (this.dgv_stu[4, rowIndex].Value.ToString() == "男")
  191. {
  192. this.rdo_man.Checked = true;
  193. }
  194. else
  195. {
  196. this.rdo_woman.Checked = true;
  197. }
  198. this.txt_address.Text = this.dgv_stu[5, rowIndex].Value.ToString();
  199. }
  200. else
  201. {
  202. MessageBox.Show("请选择有数据的行!");
  203. return;
  204. }
  205. }
  206. }
  207. //删除
  208. private void 删除ToolStripMenuItem_Click(object sender, EventArgs e)
  209. {
  210. if (Input())
  211. {
  212. int rowIndex = this.dgv_stu.CurrentCell.RowIndex;
  213. if (rowIndex>-1)
  214. {
  215. if (this.dgv_stu[0,rowIndex].Value.ToString()!=string.Empty)
  216. {
  217. string sql4 = string.Format("delete studentinfo where stu_id='{0}'",id);
  218. int rult3 = DBHelper.ExecuteNonQuery(sql4);
  219. if (rult3>-1)
  220. {
  221. MessageBox.Show("删除成功!");
  222. Bind(string.Empty, string.Empty);
  223. Clear();
  224. return;
  225. }
  226. else
  227. {
  228. MessageBox.Show("删除失败!");
  229. return;
  230. }
  231. }
  232. else
  233. {
  234. MessageBox.Show("请选择有数据的行!");
  235. return;
  236. }
  237. }
  238. }
  239. }
  240. }
  241. }

对datagridview进行增删改(B)的更多相关文章

  1. 使用DataGridView进行增删改查,并同步到数据库

    DataGridView控件具有极高的可配置性和可扩展性.它提供有大量的属性.方法和事件,能够用来对该控件的外观和行为进行自己定义.以下通过一个小样例来展示DataGridView进行增删改查,并同步 ...

  2. 浅谈dataGridView使用,以及画面布局使用属性,对datagridview进行增删改查操作,以及委托使用技巧

        通过几天的努力后,对datagridview使用作一些简要的介绍,该实例主要运用与通过对datagridview操作.对数据进行增删改查操作时,进行逻辑判断执行相关操作.简单的使用委托功能,实 ...

  3. Winform开发之DataGridView的增删改

    DataGridView是一个非常强大的控件,用法很多.这里介绍一个简单的增删改例子. 贴效果图 右侧输入学生信息点击新增,将数据增加到数据库,并且加载到datagridview中,点击选择某条数据修 ...

  4. SqlDataAdapter 对datagridview进行增删改(A)

    这种方法主要是双击datagridview单元格,直接进行添加,修改,删除,在实际开发中并不太常用,另一种方法下一次在具体陈述. using System; using System.Collecti ...

  5. winform窗体(六)——DataGridView控件及通过此控件中实现增删改查

    DataGridView:显示数据表,通过此控件中可以实现连接数据库,实现数据的增删改查 一.后台数据绑定:    List<xxx> list = new List<xxx> ...

  6. Winform(DataGridView)控件及通过此控件中实现增删改查

    DataGridView:显示数据表,通过此控件中可以实现连接数据库,实现数据的增删改查 一.后台数据绑定:    List<xxx> list = new List<xxx> ...

  7. WinForm DataGridView增删改查

    DataGridView连接数据库对表进行增删改查 一.绑定数据源 //做一个变量控制页面刷新 ; public Form1() { InitializeComponent(); } private ...

  8. datagridview数据绑定操作数据库实现增删改查

    DataSet数据集,数据缓存在客户端内存中,支持断开式连接.DataGridView控件绑定DataSet时,它自动的改变的DS的行的状态,而且在做增删改查的时候,可以借助SqlCommandBui ...

  9. c#操作数据库的增删改查语句及DataGridView简单使用

    下面是要用户名和密码连接数据库的操作: 一.定义连接字符串,用来链接SQL Server string str_con = "server=.(服务器名称一般为 . );database=W ...

随机推荐

  1. title

    事实上 @import url(http://i.cnblogs.com/Load.ashx?type=style&file=SyntaxHighlighter.css);@import ur ...

  2. RNN以及LSTM的介绍和公式梳理

    前言 好久没用正儿八经地写博客了,csdn居然也有了markdown的编辑器了,最近花了不少时间看RNN以及LSTM的论文,在组内『夜校』分享过了,再在这里总结一下发出来吧,按照我讲解的思路,理解RN ...

  3. LogisticRegression in MLLib (PySpark + numpy+matplotlib可视化)

    参考'LogisticRegression in MLLib' (http://www.cnblogs.com/luweiseu/p/7809521.html) 通过pySpark MLlib训练lo ...

  4. 【转】基于Token的WEB后台认证机制

    原谅地址:http://www.cnblogs.com/xiekeli/p/5607107.html 几种常用的认证机制 HTTP Basic Auth HTTP Basic Auth简单点说明就是每 ...

  5. 《Python自动化运维之路》 系统基础信息模块(一)

    系统性能收集模块Psutil 目录: 系统性能信息模块psutil 系统性能信息模块psutil psutil能够轻松实现获取系统运行的进程和系统利用率包括(CPU,内存,磁盘 和网络)等.主要用于系 ...

  6. [SDOI2011]消耗战(虚树+树形动规)

    虚树dp 虚树的主要思想: 不遍历没用的的节点以及没用的子树,从而使复杂度降低到\(\sum\limits k\)(k为询问的节点的总数). 所以怎么办: 只把询问节点和其LCA放入询问的数组中. 1 ...

  7. Windows 网卡超过序列

    以前连接过别的网络,现在重新装网已经排到"网络连接9"了,连以前起过的wifi名称(ssid)都变成"*** 3"了,怎么把以前的清空呢? 这是解决办法: wi ...

  8. js 标准二维数组变一维数组的方法

    问题:[[0, 1], [2, 3], [4, 5]] -> [0, 1, 2, 3, 4, 5]? 方法一 利用es5的arr.reduce(callback[, initialValue]) ...

  9. git小乌龟工具TortoiseGit记住你的账号密码

    在使用TortoiseGit的过程中,发下每次push或者pull都要重复输入账号密码,非常麻烦 怎么设置记住密码 在[系统盘]:\Users[你的用户名](比如C:\User\Administrat ...

  10. 从C#到TypeScript - 类型

    总目录 从C#到TypeScript - 类型 从C#到TypeScript - 高级类型 从C#到TypeScript - 变量 从C#到TypeScript - 接口 从C#到TypeScript ...