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. Winform DataGridView控件在业务逻辑上的简单使用

    需要对文字列表进行处理,然后用到DataGridView控件来处理,记录一下.效果如下: 主要是想通过禁用和取消单元格选择来使图标单元格呈现出鼠标点击的效果.因为有个单元格选择的问题困扰着我. 是这样 ...

  2. 新建WebAPI项目时遇到的问题

    1   处理程序“ExtensionlessUrlHandler-Integrated-4.0”在其模块列表中有一个错误模块“ManagedPipelineHandler” 以管理员运行下面的命令注册 ...

  3. 【BZOJ1052】 [HAOI2007]覆盖问题

    BZOJ1052 [HAOI2007]覆盖问题 前言 小清新思维题. 最近肯定需要一些思维题挽救我这种碰到题目只会模板的菜鸡. 这题腾空出世? Solution 考虑一下我们二分答案怎么做? 首先转换 ...

  4. python--Websocket实现, 加密 sha1,base64

    需要用到gevent-websocket包,这里我们用下图这个 一.websocket简单实现 ep1.py from geventwebsocket.handler import WebSocket ...

  5. MariaDB MyCat实现读写分离(15)

    MariaDB数据库管理系统是MySQL的一个分支,主要由开源社区在维护,采用GPL授权许可MariaDB的目的是完全兼容MySQL,包括API和命令行,MySQL由于现在闭源了,而能轻松成为MySQ ...

  6. SQL 一对多联表查询最大值

    有两个数据表City表和Price表,CIty表的结构如下: Price表的结构如下: 查询每个城市最大的销售价格,并以最大价格进行降序排列,选取前5条记录,SQL语句的代码如下: * from (s ...

  7. 浅谈 Web 缓存

    在前端开发中,性能一直都是被大家所重视的一点,然而判断一个网站的性能最直观的就是看网页打开的速度.其中提高网页反应速度的一个方式就是使用缓存.一个优秀的缓存策略可以缩短网页请求资源的距离,减少延迟,并 ...

  8. FastDFS高可用集群架构配置搭建

      一.基本模块及高可用架构 FastDFS 是余庆老师开发的一个开源的高性能分布式文件系统(DFS). 它的主要功能包括:文件存储,文件同步和文件访问,以及高容量和负载平衡. FastDFS 系统有 ...

  9. vue教程2-05 v-for循环 重复数据无法添加问题 加track-by='索引'

    vue教程2-05 v-for循环 重复数据无法添加问题  加track-by='索引' 解决问题的代码示例: <!DOCTYPE html> <html lang="en ...

  10. Servlet各种路径、URL配置分析

    大家都知道,Servlet有个配置: <servlet> <servlet-name>zolltyMVC</servlet-name> <servlet-cl ...