只需一个窗体一个dataGridView控件,即可实现系统中所有基础数据的维护操作,真是太简单实用了,还不快Get了?
1、公共类User.cs:
public abstract class DBUser { public static string sServer; public static string sDBName; public static string sUser; public static string sPasswd; } public abstract class dataGrid { public static int ROW, COL; public static string rKey, Text, PID; public static int flag; public static int Count; } public abstract class DBTable { public static int wStatus; public static string dbTableName;//表名称 public static string dbViewName;//数据库试图名称 public static string[] dbFields;//字段 public static string[] dbCFields;//中文字段,用来代替数据库试图 public static int[] fStatus;//字段是否为标识 public static bool[] IsNull;//字段是否为空 public static int FieldCount;//字段数目 public static string Proc_insert;//插入数据的存储过程 public static string Proc_update;//更新数据的存储过程 }2、Main.cs:
using System; using System.Windows.Forms; namespace 项目实战学习 { public partial class Main : Form { public Main() { InitializeComponent(); } private void 单位维护ToolStripMenuItem_Click(object sender, EventArgs e) { int n = 15; DBTable.dbTableName = "Unit_S"; DBTable.dbViewName = "View_Unit_S"; DBTable.FieldCount = n; DBTable.dbFields = new string[n]; DBTable.dbCFields = new string[n]; DBTable.fStatus = new int[n]; DBTable.IsNull = new bool[n]; DBTable.dbFields[0] = "U#"; //DBTable.dbCFields[0] = "编码"; //用于代替数据库视图 DBTable.dbFields[1] = "Uname"; //DBTable.dbCFields[1] = "定义"; DBTable.dbFields[2] = "Place"; //DBTable.dbCFields[2] = "备注"; DBTable.dbFields[3] = "PlaceCounty"; DBTable.dbFields[4] = "Code"; DBTable.dbFields[5] = "Chuanzhen"; DBTable.dbFields[6] = "People"; DBTable.dbFields[7] = "Tel"; DBTable.dbFields[8] = "Quhao"; DBTable.dbFields[9] = "Zizhi"; DBTable.dbFields[10] = "Grade"; DBTable.dbFields[11] = "DATE"; DBTable.dbFields[12] = "Address"; DBTable.dbFields[13] = "PID"; DBTable.dbFields[14] = "FaDing_People"; DBTable.Proc_insert = "InsertUnit_S"; DBTable.Proc_update = "UpdateUnit_S"; //标识列,自动增长的字段设为-1 DBTable.fStatus[0] = -1; for (int i = 1; i < 15; i++) DBTable.fStatus[i] = 1; DBTable.IsNull[0] = false;//不能为空,一般只主键 for (int i = 1; i < 15; i++) DBTable.IsNull[i] = true; Form1 f1 = new Form1(); f1.MdiParent = this; f1.Text = "单位维护"; f1.Show(); } } }3、form1.cs:
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 项目实战学习 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { RefreshGrid(); } private void dataGridView1_CellMouseDown(object sender, DataGridViewCellMouseEventArgs e) { if(e.RowIndex<0 || e.ColumnIndex<0) { dataGrid.flag = -10; return; } dataGrid.Count = dataGridView1.RowCount; dataGrid.ROW = e.RowIndex; dataGrid.COL = e.ColumnIndex; dataGrid.flag = 0; if (dataGridView1.Rows[e.RowIndex].Cells[0].Value == null) dataGrid.flag = -1; else dataGrid.rKey = dataGridView1.Rows[e.RowIndex].Cells[0].Value.ToString(); if (dataGridView1.Rows[e.RowIndex].Cells[1].Value == null) dataGrid.flag = -2; else dataGrid.Text = dataGridView1.Rows[e.RowIndex].Cells[1].Value.ToString(); } void DeleteInGrid() { if(dataGrid.flag<0) { MessageBox.Show("请选择要删除的记录"); return; } MyDBase DB = new MyDBase(DBUser.sServer, DBUser.sDBName, DBUser.sUser, DBUser.sPasswd); string SQL = "delete from "+DBTable.dbTableName+" where " + DBTable.dbFields[0] + "='" + dataGrid.rKey + "'"; DB.ExecuteSQL(SQL); DB.DBClose(); RefreshGrid(); } private void 该行写进数据库ToolStripMenuItem_Click(object sender, EventArgs e) { GridInsert(dataGrid.ROW); } void GridUpdate(int n) { if (dataGrid.flag < 0) { MessageBox.Show("没有数据的行,无法更新", "更新错误提示", MessageBoxButtons.OK, MessageBoxIcon.Stop);return; } int i, m; string SQL=""; string[] sCells = new string[DBTable.FieldCount]; for (i = 0; i < DBTable.FieldCount; i++) { if (DBTable.fStatus[i] < 0) { sCells[i] = ""; continue; } if (DBTable.fStatus[i] >= 0 && dataGridView1.Rows[n].Cells[2].Value != null) sCells[i] = dataGridView1.Rows[n].Cells[i].Value.ToString().Trim(); } for (i = 1; i < DBTable.FieldCount; i++)//不包括主键字段 { if (DBTable.fStatus[i] == 0) SQL = SQL + sCells[i] + ","; if (DBTable.fStatus[i] == 1) SQL = SQL + "'" + sCells[i] + "',"; } m = SQL.Length; SQL = SQL.Substring(0, m - 1); SQL = "exec " + DBTable.Proc_update + " "+dataGrid.rKey+"," +SQL; MyDBase DB = new MyDBase(DBUser.sServer, DBUser.sDBName, DBUser.sUser, DBUser.sPasswd); DB.ExecuteSQL(SQL); if (DB.ErrorCode()) { MessageBox.Show(DB.ErrMessage(), "数据更新错误", MessageBoxButtons.OK, MessageBoxIcon.Stop); DB.DBClose(); return; } DB.DBClose(); RefreshGrid(); } void GridInsert(int n) { int i, m; string SQL=""; string[] sCells = new string[DBTable.FieldCount]; for (i = 0; i < DBTable.FieldCount; i++) { if (DBTable.fStatus[i] < 0) { sCells[i] = ""; continue; } if (DBTable.fStatus[i] >= 0 && dataGridView1.Rows[n].Cells[1].Value != null) sCells[i] = dataGridView1.Rows[n].Cells[i].Value.ToString().Trim(); } for (i = 1; i < DBTable.FieldCount; i++) { if (DBTable.fStatus[i] == 0) SQL = SQL + sCells[i] + ",";//数值型 if (DBTable.fStatus[i] == 1) SQL = SQL + "'" + sCells[i] + "',";//字符串型 } m = SQL.Length; SQL = SQL.Substring(0, m - 1); SQL = "EXEC "+DBTable.Proc_insert+ " "+SQL; MyDBase DB = new MyDBase(DBUser.sServer, DBUser.sDBName, DBUser.sUser, DBUser.sPasswd); DB.ExecuteSQL(SQL); if (DB.ErrorCode()) { MessageBox.Show(DB.ErrMessage(), "数据插入错误", MessageBoxButtons.OK, MessageBoxIcon.Stop); DB.DBClose(); return; } DB.DBClose(); RefreshGrid(); } void RefreshGrid() { MyDBase DB = new MyDBase(DBUser.sServer, DBUser.sDBName, DBUser.sUser, DBUser.sPasswd); string SQL = "select * from " + DBTable.dbViewName; DataSet DS = DB.GetRecordset(SQL); dataGridView1.DataSource = DS.Tables[0]; DB.DBClose(); } private void 删除该行记录ToolStripMenuItem_Click(object sender, EventArgs e) { DeleteInGrid(); } private void 更新该行记录ToolStripMenuItem_Click(object sender, EventArgs e) { GridUpdate(dataGrid.ROW); } } }