UserManage.cs
上传用户:chizxy
上传日期:2014-11-29
资源大小:407k
文件大小:10k
源码类别:

其他行业

开发平台:

C#

  1. using System;
  2. using System.Drawing;
  3. using System.Collections;
  4. using System.ComponentModel;
  5. using System.Windows.Forms;
  6. using System.Data;
  7. using System.Data.SqlClient;
  8. namespace MKIms3
  9. {
  10. /// <summary>
  11. /// UserManage 的摘要说明。
  12. /// </summary>
  13. public class UserManage : System.Windows.Forms.Form
  14. {
  15. private System.Windows.Forms.DataGridBoolColumn dataGridBoolColumn;
  16. // private DataBase myDB;
  17. private DataSet user_Set;
  18. private DataGridTableStyle GridTableStyle;
  19. private System.Windows.Forms.GroupBox groupBox1;
  20. private System.Windows.Forms.Label label1;
  21. private System.Windows.Forms.TextBox txt_uname;
  22. private System.Windows.Forms.Button btn_query;
  23. private System.Windows.Forms.Button btn_all;
  24. private System.Windows.Forms.Button btn_ok;
  25. private System.Windows.Forms.Label label2;
  26. private System.Windows.Forms.DataGrid dataGrid_user;
  27. private System.Windows.Forms.PictureBox pictureBox1;
  28. /// <summary>
  29. /// 必需的设计器变量。
  30. /// </summary>
  31. private System.ComponentModel.Container components = null;
  32. public UserManage()
  33. {
  34. //
  35. // Windows 窗体设计器支持所必需的
  36. //
  37. InitializeComponent();
  38. //
  39. // TODO: 在 InitializeComponent 调用后添加任何构造函数代码
  40. //
  41. }
  42. /// <summary>
  43. /// 清理所有正在使用的资源。
  44. /// </summary>
  45. protected override void Dispose( bool disposing )
  46. {
  47. if( disposing )
  48. {
  49. if(components != null)
  50. {
  51. components.Dispose();
  52. }
  53. }
  54. base.Dispose( disposing );
  55. }
  56. #region Windows 窗体设计器生成的代码
  57. /// <summary>
  58. /// 设计器支持所需的方法 - 不要使用代码编辑器修改
  59. /// 此方法的内容。
  60. /// </summary>
  61. private void InitializeComponent()
  62. {
  63. System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(UserManage));
  64. this.dataGrid_user = new System.Windows.Forms.DataGrid();
  65. this.groupBox1 = new System.Windows.Forms.GroupBox();
  66. this.btn_all = new System.Windows.Forms.Button();
  67. this.btn_query = new System.Windows.Forms.Button();
  68. this.txt_uname = new System.Windows.Forms.TextBox();
  69. this.label1 = new System.Windows.Forms.Label();
  70. this.btn_ok = new System.Windows.Forms.Button();
  71. this.label2 = new System.Windows.Forms.Label();
  72. this.pictureBox1 = new System.Windows.Forms.PictureBox();
  73. ((System.ComponentModel.ISupportInitialize)(this.dataGrid_user)).BeginInit();
  74. this.groupBox1.SuspendLayout();
  75. this.SuspendLayout();
  76. // 
  77. // dataGrid_user
  78. // 
  79. this.dataGrid_user.AlternatingBackColor = System.Drawing.SystemColors.Info;
  80. this.dataGrid_user.CaptionText = "用户列表";
  81. this.dataGrid_user.DataMember = "";
  82. this.dataGrid_user.HeaderForeColor = System.Drawing.SystemColors.ControlText;
  83. this.dataGrid_user.Location = new System.Drawing.Point(176, 56);
  84. this.dataGrid_user.Name = "dataGrid_user";
  85. this.dataGrid_user.PreferredColumnWidth = 90;
  86. this.dataGrid_user.Size = new System.Drawing.Size(312, 160);
  87. this.dataGrid_user.TabIndex = 4;
  88. // 
  89. // groupBox1
  90. // 
  91. this.groupBox1.Controls.Add(this.btn_all);
  92. this.groupBox1.Controls.Add(this.btn_query);
  93. this.groupBox1.Controls.Add(this.txt_uname);
  94. this.groupBox1.Controls.Add(this.label1);
  95. this.groupBox1.Location = new System.Drawing.Point(176, 8);
  96. this.groupBox1.Name = "groupBox1";
  97. this.groupBox1.Size = new System.Drawing.Size(312, 40);
  98. this.groupBox1.TabIndex = 1;
  99. this.groupBox1.TabStop = false;
  100. this.groupBox1.Text = "操作区";
  101. // 
  102. // btn_all
  103. // 
  104. this.btn_all.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
  105. this.btn_all.Location = new System.Drawing.Point(232, 11);
  106. this.btn_all.Name = "btn_all";
  107. this.btn_all.Size = new System.Drawing.Size(75, 24);
  108. this.btn_all.TabIndex = 3;
  109. this.btn_all.Text = "浏览全部";
  110. this.btn_all.Click += new System.EventHandler(this.btn_all_Click);
  111. // 
  112. // btn_query
  113. // 
  114. this.btn_query.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
  115. this.btn_query.Location = new System.Drawing.Point(167, 11);
  116. this.btn_query.Name = "btn_query";
  117. this.btn_query.Size = new System.Drawing.Size(56, 24);
  118. this.btn_query.TabIndex = 2;
  119. this.btn_query.Text = "查询";
  120. this.btn_query.Click += new System.EventHandler(this.btn_query_Click);
  121. // 
  122. // txt_uname
  123. // 
  124. this.txt_uname.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
  125. this.txt_uname.Location = new System.Drawing.Point(61, 12);
  126. this.txt_uname.Name = "txt_uname";
  127. this.txt_uname.TabIndex = 1;
  128. this.txt_uname.Text = "";
  129. // 
  130. // label1
  131. // 
  132. this.label1.Location = new System.Drawing.Point(5, 12);
  133. this.label1.Name = "label1";
  134. this.label1.Size = new System.Drawing.Size(56, 23);
  135. this.label1.TabIndex = 0;
  136. this.label1.Text = "用户名:";
  137. this.label1.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
  138. // 
  139. // btn_ok
  140. // 
  141. this.btn_ok.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
  142. this.btn_ok.Location = new System.Drawing.Point(400, 222);
  143. this.btn_ok.Name = "btn_ok";
  144. this.btn_ok.Size = new System.Drawing.Size(64, 24);
  145. this.btn_ok.TabIndex = 2;
  146. this.btn_ok.Text = "提交修改";
  147. this.btn_ok.Click += new System.EventHandler(this.btn_ok_Click);
  148. // 
  149. // label2
  150. // 
  151. this.label2.Location = new System.Drawing.Point(13, 224);
  152. this.label2.Name = "label2";
  153. this.label2.Size = new System.Drawing.Size(392, 23);
  154. this.label2.TabIndex = 3;
  155. this.label2.Text = "系统提示:如果您对用户的资料进行了修改请提交修改以更新用户数据";
  156. this.label2.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
  157. // 
  158. // pictureBox1
  159. // 
  160. this.pictureBox1.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
  161. this.pictureBox1.Image = ((System.Drawing.Image)(resources.GetObject("pictureBox1.Image")));
  162. this.pictureBox1.Location = new System.Drawing.Point(16, 16);
  163. this.pictureBox1.Name = "pictureBox1";
  164. this.pictureBox1.Size = new System.Drawing.Size(144, 200);
  165. this.pictureBox1.TabIndex = 5;
  166. this.pictureBox1.TabStop = false;
  167. // 
  168. // UserManage
  169. // 
  170. this.AcceptButton = this.btn_query;
  171. this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
  172. this.ClientSize = new System.Drawing.Size(496, 253);
  173. this.Controls.Add(this.pictureBox1);
  174. this.Controls.Add(this.btn_ok);
  175. this.Controls.Add(this.label2);
  176. this.Controls.Add(this.groupBox1);
  177. this.Controls.Add(this.dataGrid_user);
  178. this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedToolWindow;
  179. this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon")));
  180. this.Name = "UserManage";
  181. this.ShowInTaskbar = false;
  182. this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
  183. this.Text = "系统用户管理";
  184. this.Load += new System.EventHandler(this.UserManage_Load);
  185. ((System.ComponentModel.ISupportInitialize)(this.dataGrid_user)).EndInit();
  186. this.groupBox1.ResumeLayout(false);
  187. this.ResumeLayout(false);
  188. }
  189. #endregion
  190. #region  设置datagrid的列标题
  191. private void add_style(DataSet user_set)
  192. {
  193. CurrencyManager objcengCM;
  194. GridTableStyle = new DataGridTableStyle();
  195. GridTableStyle.MappingName = "users";
  196. objcengCM = (System.Windows.Forms.CurrencyManager)this.BindingContext[user_set.Tables[0]];
  197. GridTableStyle.GridColumnStyles.Add(new DataGridTextBoxColumn(objcengCM .GetItemProperties()["用户名"]));
  198. GridTableStyle.GridColumnStyles[0].MappingName = "用户名";
  199. GridTableStyle.GridColumnStyles[0].HeaderText = "用户名 ";
  200. GridTableStyle.GridColumnStyles[0].Width= 550;
  201. GridTableStyle.GridColumnStyles[0].Alignment = HorizontalAlignment.Left;
  202. GridTableStyle.GridColumnStyles[0].NullText = string.Empty;
  203. GridTableStyle.GridColumnStyles.Add(new DataGridTextBoxColumn(objcengCM .GetItemProperties()["用户密码"]));
  204. GridTableStyle.GridColumnStyles[1].MappingName = "用户密码";
  205. GridTableStyle.GridColumnStyles[1].HeaderText = "用户密码 ";
  206. GridTableStyle.RowHeaderWidth = 250;
  207. GridTableStyle.GridColumnStyles[1].Alignment = HorizontalAlignment.Left;
  208. GridTableStyle.GridColumnStyles[1].NullText = string.Empty;
  209. this.dataGridBoolColumn = new System.Windows.Forms.DataGridBoolColumn();
  210.             dataGridBoolColumn.MappingName = "是否管理员";
  211. dataGridBoolColumn.FalseValue = 1;
  212. dataGridBoolColumn.TrueValue = 0;
  213. GridTableStyle.GridColumnStyles.Add(this.dataGridBoolColumn);
  214. GridTableStyle.GridColumnStyles[2].MappingName = "是否管理员";
  215. GridTableStyle.GridColumnStyles[2].HeaderText = "是否管理员";
  216. GridTableStyle.GridColumnStyles[2].Width = 50;
  217. GridTableStyle.GridColumnStyles[2].Alignment = HorizontalAlignment.Left;
  218. GridTableStyle.GridColumnStyles[2].NullText = string.Empty;
  219. }
  220. #endregion
  221. #region  得到所有的用户或者查询指定用户 ,提交更新
  222. /// <summary>
  223. /// 根据用户名得到指定的用户
  224. /// </summary>
  225. /// <param name="u_name">如果参数为""(空) 表示为所有用户</param>
  226. /// <returns></returns>
  227. private DataSet get_user(string u_name)
  228. {
  229. DataBase myDB = new DataBase();
  230. DataSet dataset = new DataSet();
  231. SqlParameter[] myParamers={new SqlParameter("@u_name",SqlDbType.Char,10)} ;
  232. myParamers[0].Value = u_name;
  233. dataset = myDB.RunProcedure("up_get_users",myParamers,"users");
  234. return dataset;
  235. }
  236. private bool update_user()
  237. {
  238. DataBase myDB = new DataBase();
  239. string select = "select u_name as 用户名,u_pass as 用户密码 , u_flag as 是否管理员 from users ";
  240. if(myDB.Run_change(select,this.user_Set,"users"))
  241. {
  242. return true;
  243. }
  244. else
  245. {
  246. return false;
  247. }
  248. }
  249. #endregion
  250. private void UserManage_Load(object sender, System.EventArgs e)
  251. {
  252.    user_Set = new DataSet();
  253.    this.user_Set = get_user("");
  254.    add_style(user_Set);
  255.    this.dataGrid_user.SetDataBinding(user_Set,"users");
  256. }
  257. private void btn_query_Click(object sender, System.EventArgs e)
  258. {
  259. string u_name = this.txt_uname.Text.Trim();
  260. this.user_Set = get_user(u_name);
  261. this.dataGrid_user.SetDataBinding(user_Set,"users");
  262. }
  263. private void btn_all_Click(object sender, System.EventArgs e)
  264. {
  265. this.user_Set = get_user("");
  266. this.dataGrid_user.SetDataBinding(user_Set,"users");
  267. }
  268. private void btn_ok_Click(object sender, System.EventArgs e)
  269. {
  270. if(update_user())
  271. {
  272. MessageBox.Show("用户数据更新成功","提示信息",MessageBoxButtons.OK,MessageBoxIcon.Information);
  273. }
  274. else
  275. {
  276. MessageBox.Show("用户数据更新失败","错误信息",MessageBoxButtons.OK,MessageBoxIcon.Warning);
  277. }
  278. }
  279. }
  280. }