Код программы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 Storage_Coursework { public partial class FormLogon: Form { public FormLogon() { InitializeComponent(); }
public event EventHandler UserLogining;
private void btIn_Click(object sender, EventArgs e) { string uName = tbUserName.Text.Trim(); string uPassword = tbUserPassword.Text.Trim(); var user = Storage_Coursework.Providers.UserProvider.GetUser(uName, uPassword); UserLogining(user, new EventArgs()); this.Close(); } } }
namespace Storage_Coursework { partial class FormLogon { private System.ComponentModel.IContainer components = null;
protected override void Dispose(bool disposing) { if (disposing && (components!= null)) { components.Dispose(); } base.Dispose(disposing); }
#region Windows Form Designer generated code
private void InitializeComponent() { this.gbLogon = new System.Windows.Forms.GroupBox(); this.lbUserName = new System.Windows.Forms.Label(); this.lbUserPassword = new System.Windows.Forms.Label(); this.tbUserName = new System.Windows.Forms.TextBox(); this.tbUserPassword = new System.Windows.Forms.TextBox(); this.btIn = new System.Windows.Forms.Button(); this.gbLogon.SuspendLayout(); this.SuspendLayout(); this.gbLogon.Anchor = System.Windows.Forms.AnchorStyles.None; this.gbLogon.Controls.Add(this.btIn); this.gbLogon.Controls.Add(this.tbUserPassword); this.gbLogon.Controls.Add(this.tbUserName); this.gbLogon.Controls.Add(this.lbUserPassword); this.gbLogon.Controls.Add(this.lbUserName); this.gbLogon.Location = new System.Drawing.Point(12, 12); this.gbLogon.Name = "gbLogon"; this.gbLogon.Size = new System.Drawing.Size(425, 145); this.gbLogon.TabIndex = 0; this.gbLogon.TabStop = false; this.gbLogon.Text = "Учётные данные"; this.lbUserName.AutoSize = true; this.lbUserName.Location = new System.Drawing.Point(6, 35); this.lbUserName.Name = "lbUserName"; this.lbUserName.Size = new System.Drawing.Size(111, 19); this.lbUserName.TabIndex = 0; this.lbUserName.Text = "Пользователь"; this.lbUserPassword.AutoSize = true; this.lbUserPassword.Location = new System.Drawing.Point(6, 69); this.lbUserPassword.Name = "lbUserPassword"; this.lbUserPassword.Size = new System.Drawing.Size(63, 19); this.lbUserPassword.TabIndex = 1; this.lbUserPassword.Text = "Пароль"; this.tbUserName.Location = new System.Drawing.Point(153, 32); this.tbUserName.Name = "tbUserName"; this.tbUserName.Size = new System.Drawing.Size(266, 27); this.tbUserName.TabIndex = 2; this.tbUserName.TextAlign = System.Windows.Forms.HorizontalAlignment.Right; this.tbUserPassword.Location = new System.Drawing.Point(153, 66); this.tbUserPassword.Name = "tbUserPassword"; this.tbUserPassword.Size = new System.Drawing.Size(266, 27); this.tbUserPassword.TabIndex = 3; this.tbUserPassword.TextAlign = System.Windows.Forms.HorizontalAlignment.Right; this.tbUserPassword.UseSystemPasswordChar = true; this.btIn.Location = new System.Drawing.Point(330, 99); this.btIn.Name = "btIn"; this.btIn.Size = new System.Drawing.Size(89, 34); this.btIn.TabIndex = 4; this.btIn.Text = "Вход"; this.btIn.UseVisualStyleBackColor = true; this.btIn.Click += new System.EventHandler(this.btIn_Click); this.AutoScaleDimensions = new System.Drawing.SizeF(9F, 19F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; this.ClientSize = new System.Drawing.Size(449, 169); this.Controls.Add(this.gbLogon); this.Font = new System.Drawing.Font("Tahoma", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(204))); this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedToolWindow; this.Margin = new System.Windows.Forms.Padding(4, 5, 4, 5); this.Name = "FormLogon"; this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen; this.Text = "Вход"; this.gbLogon.ResumeLayout(false); this.gbLogon.PerformLayout(); this.ResumeLayout(false);
}
#endregion
private System.Windows.Forms.GroupBox gbLogon; private System.Windows.Forms.Button btIn; private System.Windows.Forms.TextBox tbUserPassword; private System.Windows.Forms.TextBox tbUserName; private System.Windows.Forms.Label lbUserPassword; private System.Windows.Forms.Label lbUserName; } }
namespace Storage_Coursework { public partial class FormMain: Form { private Storage_Coursework.Objects.User currentUser;
public FormMain() { InitializeComponent(); }
public void UserIn() { FormLogon fl = new FormLogon(); fl.UserLogining += fl_UserLogining; fl.ShowDialog(this); }
private void fl_UserLogining(object sender, EventArgs e) { if (sender == null) { MessageBox.Show("У Вас нет прав доступа к приложению"); } else currentUser = (Storage_Coursework.Objects.User)sender; }
private void FormMain_Load(object sender, EventArgs e) { UserIn(); SetupControls(); }
private void SetupControls() { if (currentUser == null) { this.Close(); return; } slbUser.Text = "Пользователь: " + currentUser.UserName + " [" + currentUser.UserRole.RoleName + "]"; pnlStorage.Visible = currentUser.UserRole.ExistAccess("StorageView"); btAdmin.Visible = currentUser.UserRole.ExistAccess("ShowAdminPanel");
LoadStorage(); }
private void LoadStorage() { var mtrls = Storage_Coursework.Providers.MaterialProvider.GetMaterials(); List<Control> cntrls = new List<Control>(); foreach (Storage_Coursework.Objects.Material m in mtrls) { var c = new Storage_Coursework.UserControls.Material(m, currentUser); c.Dock = DockStyle.Top; cntrls.Add(c); } pnlStorage.Controls.AddRange(cntrls.ToArray()); }
private void btAdmin_Click(object sender, EventArgs e) { MessageBox.Show("Администратор имеет права:" + Environment.NewLine + "- добавления/удаления пользователей;" + Environment.NewLine + "- редактирование ролей пользователей;" + Environment.NewLine + "- настройка приложения." + Environment.NewLine + "В рамках данной курсовой работы этот функционал не разрабатывался."); } } }
namespace Storage_Coursework { partial class FormMain { private System.ComponentModel.IContainer components = null;
protected override void Dispose(bool disposing) { if (disposing && (components!= null)) { components.Dispose(); } base.Dispose(disposing); }
#region Код, автоматически созданный конструктором форм Windows
private void InitializeComponent() { this.statusStrip1 = new System.Windows.Forms.StatusStrip(); this.slbUser = new System.Windows.Forms.ToolStripStatusLabel(); this.pnlStorage = new System.Windows.Forms.Panel(); this.lbMaterials = new System.Windows.Forms.Label(); this.btAdmin = new System.Windows.Forms.Button(); this.statusStrip1.SuspendLayout(); this.SuspendLayout(); this.statusStrip1.Items.AddRange(new System.Windows.Forms.ToolStripItem[] { this.slbUser}); this.statusStrip1.Location = new System.Drawing.Point(0, 451); this.statusStrip1.Name = "statusStrip1"; this.statusStrip1.Size = new System.Drawing.Size(660, 22); this.statusStrip1.TabIndex = 0; this.statusStrip1.Text = "statusStrip1";
this.slbUser.Name = "slbUser"; this.slbUser.Size = new System.Drawing.Size(87, 17); this.slbUser.Text = "Пользователь:"; this.pnlStorage.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom) | System.Windows.Forms.AnchorStyles.Left) | System.Windows.Forms.AnchorStyles.Right))); this.pnlStorage.AutoScroll = true; this.pnlStorage.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle; this.pnlStorage.Location = new System.Drawing.Point(12, 31); this.pnlStorage.Name = "pnlStorage"; this.pnlStorage.Size = new System.Drawing.Size(636, 417); this.pnlStorage.TabIndex = 1; this.lbMaterials.AutoSize = true; this.lbMaterials.Font = new System.Drawing.Font("Tahoma", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(204))); this.lbMaterials.Location = new System.Drawing.Point(12, 9); this.lbMaterials.Name = "lbMaterials"; this.lbMaterials.Size = new System.Drawing.Size(167, 19); this.lbMaterials.TabIndex = 2; this.lbMaterials.Text = "Материалы на складе"; this.btAdmin.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right))); this.btAdmin.Location = new System.Drawing.Point(501, 2); this.btAdmin.Name = "btAdmin"; this.btAdmin.Size = new System.Drawing.Size(147, 26); this.btAdmin.TabIndex = 3; this.btAdmin.Text = "Администрирование"; this.btAdmin.UseVisualStyleBackColor = true; this.btAdmin.Click += new System.EventHandler(this.btAdmin_Click); this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; this.ClientSize = new System.Drawing.Size(660, 473); this.Controls.Add(this.btAdmin); this.Controls.Add(this.lbMaterials); this.Controls.Add(this.pnlStorage); this.Controls.Add(this.statusStrip1); this.Name = "FormMain"; this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen; this.Text = "Склад"; this.Load += new System.EventHandler(this.FormMain_Load); this.statusStrip1.ResumeLayout(false); this.statusStrip1.PerformLayout(); this.ResumeLayout(false); this.PerformLayout();
}
#endregion
private System.Windows.Forms.StatusStrip statusStrip1; private System.Windows.Forms.ToolStripStatusLabel slbUser; private System.Windows.Forms.Panel pnlStorage; private System.Windows.Forms.Label lbMaterials; private System.Windows.Forms.Button btAdmin; } }
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 Storage_Coursework { public partial class FormMatCount: Form { private Storage_Coursework.Objects.Material material; private int matCount; private int delta; private int total;
public int Total { get { return total; } set { total = value; txtTotal.Text = total + " " + material.MaterialMeasure; } }
public event EventHandler CountChanged;
public FormMatCount(Storage_Coursework.Objects.Material m, int d) { InitializeComponent(); material = m; matCount = material.StorageCount; delta = d; }
private void FormMatCount_Load(object sender, EventArgs e) { SetupControls(); }
private void SetupControls() { txtCount.Text = matCount + " " + material.MaterialMeasure; txtTotal.Text = txtCount.Text; nudInOut.ValueChanged += nudInOut_ValueChanged;
if (delta > 0) { this.Text = "Добавить материал"; lbInOut.Text = "Ввод на склад"; lbTotal.Text = "Станет";
} else { this.Text = "Вывести материал"; lbInOut.Text = "Вывод со склада"; lbTotal.Text = "Останется"; } }
void nudInOut_ValueChanged(object sender, EventArgs e) { if (delta > 0) { Total = matCount + (int)nudInOut.Value; } else { if ((int)nudInOut.Value > matCount) nudInOut.Value = matCount; Total = matCount - (int)nudInOut.Value; } }
private void btOk_Click(object sender, EventArgs e) { if ((int)nudInOut.Value!= 0) { Storage_Coursework.Providers.MaterialProvider.UpdateMaterialCount(this.material.MaterialID, this.total); CountChanged(null, null); } this.Close(); } } }
namespace Storage_Coursework { partial class FormMatCount {
private System.ComponentModel.IContainer components = null;
protected override void Dispose(bool disposing) { if (disposing && (components!= null)) { components.Dispose(); } base.Dispose(disposing); }
#region Windows Form Designer generated code
private void InitializeComponent() { this.gbMaterial = new System.Windows.Forms.GroupBox(); this.btOk = new System.Windows.Forms.Button(); this.nudInOut = new System.Windows.Forms.NumericUpDown(); this.txtTotal = new System.Windows.Forms.Label(); this.txtCount = new System.Windows.Forms.Label(); this.lbTotal = new System.Windows.Forms.Label(); this.lbInOut = new System.Windows.Forms.Label(); this.lbStorage = new System.Windows.Forms.Label(); this.gbMaterial.SuspendLayout(); ((System.ComponentModel.ISupportInitialize)(this.nudInOut)).BeginInit(); this.SuspendLayout(); this.gbMaterial.Anchor = System.Windows.Forms.AnchorStyles.None; this.gbMaterial.Controls.Add(this.btOk); this.gbMaterial.Controls.Add(this.nudInOut); this.gbMaterial.Controls.Add(this.txtTotal); this.gbMaterial.Controls.Add(this.txtCount); this.gbMaterial.Controls.Add(this.lbTotal); this.gbMaterial.Controls.Add(this.lbInOut); this.gbMaterial.Controls.Add(this.lbStorage); this.gbMaterial.Location = new System.Drawing.Point(12, 12); this.gbMaterial.Name = "gbMaterial"; this.gbMaterial.Size = new System.Drawing.Size(448, 171); this.gbMaterial.TabIndex = 0; this.gbMaterial.TabStop = false; this.gbMaterial.Text = "Материал"; this.btOk.Location = new System.Drawing.Point(331, 131); this.btOk.Name = "btOk"; this.btOk.Size = new System.Drawing.Size(111, 34); this.btOk.TabIndex = 6; this.btOk.Text = "Выполнить"; this.btOk.UseVisualStyleBackColor = true; this.btOk.Click += new System.EventHandler(this.btOk_Click); this.nudInOut.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right))); this.nudInOut.Location = new System.Drawing.Point(340, 63); this.nudInOut.Maximum = new decimal(new int[] { 100000, 0, 0, 0}); this.nudInOut.Name = "nudInOut"; this.nudInOut.Size = new System.Drawing.Size(102, 27); this.nudInOut.TabIndex = 5; this.nudInOut.TextAlign = System.Windows.Forms.HorizontalAlignment.Right; this.txtTotal.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right))); this.txtTotal.Location = new System.Drawing.Point(260, 96); this.txtTotal.Name = "txtTotal"; this.txtTotal.Size = new System.Drawing.Size(182, 19); this.txtTotal.TabIndex = 4; this.txtTotal.Text = "0"; this.txtTotal.TextAlign = System.Drawing.ContentAlignment.TopRight; this.txtCount.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right))); this.txtCount.Location = new System.Drawing.Point(256, 34); this.txtCount.Name = "txtCount"; this.txtCount.Size = new System.Drawing.Size(186, 19); this.txtCount.TabIndex = 3; this.txtCount.Text = "0"; this.txtCount.TextAlign = System.Drawing.ContentAlignment.TopRight; this.lbTotal.AutoSize = true; this.lbTotal.Location = new System.Drawing.Point(6, 96); this.lbTotal.Name = "lbTotal"; this.lbTotal.Size = new System.Drawing.Size(127, 19); this.lbTotal.TabIndex = 2; this.lbTotal.Text = "Будет на складе"; this.lbInOut.AutoSize = true; this.lbInOut.Location = new System.Drawing.Point(6, 65); this.lbInOut.Name = "lbInOut"; this.lbInOut.Size = new System.Drawing.Size(112, 19); this.lbInOut.TabIndex = 1; this.lbInOut.Text = "Ввод на склад"; this.lbStorage.AutoSize = true; this.lbStorage.Location = new System.Drawing.Point(6, 34); this.lbStorage.Name = "lbStorage"; this.lbStorage.Size = new System.Drawing.Size(145, 19); this.lbStorage.TabIndex = 0; this.lbStorage.Text = "Имеется на складе"; this.AutoScaleDimensions = new System.Drawing.SizeF(9F, 19F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; this.ClientSize = new System.Drawing.Size(472, 195); this.Controls.Add(this.gbMaterial); this.Font = new System.Drawing.Font("Tahoma", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(204))); this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedToolWindow; this.Margin = new System.Windows.Forms.Padding(4); this.Name = "FormMatCount"; this.Text = "Добавить"; this.Load += new System.EventHandler(this.FormMatCount_Load); this.gbMaterial.ResumeLayout(false); this.gbMaterial.PerformLayout(); ((System.ComponentModel.ISupportInitialize)(this.nudInOut)).EndInit(); this.ResumeLayout(false);
}
#endregion
private System.Windows.Forms.GroupBox gbMaterial; private System.Windows.Forms.Button btOk; private System.Windows.Forms.NumericUpDown nudInOut; private System.Windows.Forms.Label txtTotal; private System.Windows.Forms.Label txtCount; private System.Windows.Forms.Label lbTotal; private System.Windows.Forms.Label lbInOut; private System.Windows.Forms.Label lbStorage; } } using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Data;
namespace Storage_Coursework { public class ObjectProvider { public ObjectProvider() {
}
public static object GetObject(DataRow r, Type objectType) { System.Reflection.ConstructorInfo cnstr = objectType.GetConstructor(System.Type.EmptyTypes); var o = cnstr.Invoke(null); System.Reflection.MemberInfo[] members = objectType.GetMembers(); int fieldsCount = r.Table.Columns.Count; for (int i = 0; i < fieldsCount; i++) { string fieldName = r.Table.Columns[i].ColumnName; object fieldValue = r[fieldName];
var member = members.First(t => t.Name == fieldName && t.MemberType == System.Reflection.MemberTypes.Property); if (member!= null) { System.Reflection.PropertyInfo property = (System.Reflection.PropertyInfo)member; Type t = property.PropertyType; object val; if (t.Name == "String" || t.Name == "Char") if (fieldValue.ToString()!= "") val = fieldValue; else val = null; else { if (fieldValue.ToString() == "") val = null; else { System.Reflection.MethodInfo parseMethod = t.GetMethod("Parse", new Type[] { "s".GetType() }); val = parseMethod.Invoke(o, new object[] { fieldValue.ToString() }); } } property.SetValue(o, val, null); } } return o; } }
}
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Data; using System.Data.OleDb;
namespace Storage_Coursework { public static class OleDB { private static OleDbConnection ole; static OleDB() { string connStr = "Provider=Microsoft.Ace.OLEDB.12.0; Data Source=Storage_CourseworkDB.accdb"; ole = new OleDbConnection(connStr); ole.Open(); }
public static DataSet ExecuteQuery(string query) { try { OleDbDataAdapter adapter = new OleDbDataAdapter(query, ole); DataSet ds = new DataSet("Data"); adapter.FillSchema(ds, SchemaType.Source, "Table"); adapter.Fill(ds, "Table"); return ds; } catch (Exception e) { return null; } }
public static bool DsIsNullOrEmpty(DataSet ds) { if (ds == null || ds.Tables[0].Rows.Count == 0) return true; return false; } }
}
using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using System.Windows.Forms;
namespace Storage_Coursework { static class Program {
[STAThread] static void Main() { Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); Application.Run(new FormMain()); } } }
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Data; using System.Data.SqlClient;
namespace Storage_Coursework { public static class SqlDB { private static SqlConnection sql; static SqlDB() { string connStr = "Data Source=TIMOFEYPC\\SQLEXPRESS;Database=Storage_Coursework;Integrated Security=false;User ID=sa; Password=123123"; sql = new SqlConnection(connStr); sql.Open(); }
public static DataSet ExecuteQuery(string query) { try { SqlDataAdapter adapter = new SqlDataAdapter(query, sql); DataSet ds = new DataSet("Data"); adapter.FillSchema(ds, SchemaType.Source, "Table"); adapter.Fill(ds, "Table"); return ds; } catch (Exception e) { return null; } }
public static bool DsIsNullOrEmpty(DataSet ds) { if (ds == null || ds.Tables[0].Rows.Count == 0) return true; return false; } } }
Заключение При разработке приложения «Склад строительных материалов» мной были учтены такие пожелания Заказчика как простота использования и удобство интерфейса, а также защищенность от постороннего вмешательства. Также были достигнуты цели курсовой работы. Удалось создать многоуровневую архитектуру приложения, разработать и отладить метод безошибочной идентификации пользователей системы. Приложение получилось многопользовательским с распределением прав пользователей в зависимости от присвоенных ролей. В процессе выполнения этой курсовой работы мной был разработан нестандартный метод преобразования объектов базы данных в объекты классов приложения. Такой подход позволит упростить дальнейшую разработку и сопровождение программы «Склад строительных материалов» и расширить её до масштабов полноценной многофункциональной информационной системы для строительного предприятия.
Список литературы 1. Михаил Флёнов. Библия C#. 2-е издание. БХВ-Петербург. 2011 2. Эндрю Троелсен. Язык программирования C# 2010. Вильямс. 2011 3. http://ru.wikibooks.org Объектно-ориентированное программирование 4. http://habrahabr.ru/post/150041 Отношения классов – от UML к коду. 5. http://hashcode.ru/questions/87801 MS Acces и C# 6. http://c-xx.narod.ru/ipsz2.htm Многоуровневая структура приложения
|