📄 createtable.cs
字号:
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Data.SqlClient;
using DBMaintainDll.Data_Structure;
namespace DBMaintainDll
{
/// <summary>
/// CreateTable 的摘要说明。
/// </summary>
public class CreateTable : System.Windows.Forms.Form
{
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null;
private System.Windows.Forms.DataGrid dataGrid1;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.TextBox textBox1;
private System.Windows.Forms.ComboBox comboBox1;
private System.Windows.Forms.TextBox textBox2;
private System.Windows.Forms.Label label3;
private System.Windows.Forms.CheckBox checkBox1;
private System.Windows.Forms.Button btnDel;
private System.Windows.Forms.Button btnAdd;
private System.Windows.Forms.Button btnSav;
public DataAccess da =new DataAccess();
public DataTable dt=null;
public DataRowCollection drc=null;
public int flagadd=0;
public int flagdel=0;
public DataTable dtdel=null;
public DataTable dtadd=null;
public int preop=0;//记录上一次的操作
public DataTable dt1=null;//记录修改时 的原始数据
private System.Windows.Forms.Label label4;
private System.Windows.Forms.TextBox textBox3;
private System.Windows.Forms.GroupBox groupBox1;
private System.Windows.Forms.Label label2;
private System.Windows.Forms.Label label5;
private System.Windows.Forms.GroupBox groupBox2;
public DataRowCollection drcdel=null;
public ArrayList tables=DBMaintainFrm.tables;
public int type=0;
private System.Windows.Forms.Button btnModify;
public String tablename=null;
public bool created;
public CreateTable(string tablename)
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent();
this.tablename=tablename;
//
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
//
}
/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if(components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows 窗体设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.btnDel = new System.Windows.Forms.Button();
this.btnModify = new System.Windows.Forms.Button();
this.dataGrid1 = new System.Windows.Forms.DataGrid();
this.label1 = new System.Windows.Forms.Label();
this.textBox1 = new System.Windows.Forms.TextBox();
this.comboBox1 = new System.Windows.Forms.ComboBox();
this.textBox2 = new System.Windows.Forms.TextBox();
this.label3 = new System.Windows.Forms.Label();
this.checkBox1 = new System.Windows.Forms.CheckBox();
this.btnAdd = new System.Windows.Forms.Button();
this.btnSav = new System.Windows.Forms.Button();
this.label4 = new System.Windows.Forms.Label();
this.textBox3 = new System.Windows.Forms.TextBox();
this.groupBox1 = new System.Windows.Forms.GroupBox();
this.label5 = new System.Windows.Forms.Label();
this.label2 = new System.Windows.Forms.Label();
this.groupBox2 = new System.Windows.Forms.GroupBox();
((System.ComponentModel.ISupportInitialize)(this.dataGrid1)).BeginInit();
this.groupBox1.SuspendLayout();
this.groupBox2.SuspendLayout();
this.SuspendLayout();
//
// btnDel
//
this.btnDel.Location = new System.Drawing.Point(16, 64);
this.btnDel.Name = "btnDel";
this.btnDel.Size = new System.Drawing.Size(56, 32);
this.btnDel.TabIndex = 1;
this.btnDel.Text = "删除";
this.btnDel.Click += new System.EventHandler(this.btnDel_Click);
//
// btnModify
//
this.btnModify.Location = new System.Drawing.Point(16, 112);
this.btnModify.Name = "btnModify";
this.btnModify.Size = new System.Drawing.Size(56, 32);
this.btnModify.TabIndex = 2;
this.btnModify.Text = "修改";
this.btnModify.Click += new System.EventHandler(this.btnCan_Click);
//
// dataGrid1
//
this.dataGrid1.AlternatingBackColor = System.Drawing.Color.LightBlue;
this.dataGrid1.CaptionBackColor = System.Drawing.Color.LightSkyBlue;
this.dataGrid1.DataMember = "";
this.dataGrid1.HeaderForeColor = System.Drawing.SystemColors.ControlText;
this.dataGrid1.Location = new System.Drawing.Point(8, 8);
this.dataGrid1.Name = "dataGrid1";
this.dataGrid1.PreferredColumnWidth = 89;
this.dataGrid1.ReadOnly = true;
this.dataGrid1.RowHeadersVisible = false;
this.dataGrid1.SelectionBackColor = System.Drawing.Color.FromArgb(((System.Byte)(0)), ((System.Byte)(0)), ((System.Byte)(192)));
this.dataGrid1.Size = new System.Drawing.Size(360, 208);
this.dataGrid1.TabIndex = 3;
this.dataGrid1.MouseDown += new System.Windows.Forms.MouseEventHandler(this.dataGrid1_MouseDown);
this.dataGrid1.MouseUp += new System.Windows.Forms.MouseEventHandler(this.dataGrid1_MouseUp);
//
// label1
//
this.label1.Location = new System.Drawing.Point(8, 56);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(64, 16);
this.label1.TabIndex = 4;
this.label1.Text = "列 名:";
//
// textBox1
//
this.textBox1.Location = new System.Drawing.Point(80, 56);
this.textBox1.Name = "textBox1";
this.textBox1.Size = new System.Drawing.Size(144, 21);
this.textBox1.TabIndex = 5;
this.textBox1.Text = "";
//
// comboBox1
//
this.comboBox1.Items.AddRange(new object[] {
"int",
"float",
"nvarchar"});
this.comboBox1.Location = new System.Drawing.Point(80, 96);
this.comboBox1.Name = "comboBox1";
this.comboBox1.Size = new System.Drawing.Size(112, 20);
this.comboBox1.TabIndex = 7;
this.comboBox1.SelectedIndexChanged += new System.EventHandler(this.comboBox1_SelectedIndexChanged);
//
// textBox2
//
this.textBox2.Location = new System.Drawing.Point(80, 128);
this.textBox2.Name = "textBox2";
this.textBox2.Size = new System.Drawing.Size(40, 21);
this.textBox2.TabIndex = 8;
this.textBox2.Text = "";
//
// label3
//
this.label3.Location = new System.Drawing.Point(8, 136);
this.label3.Name = "label3";
this.label3.Size = new System.Drawing.Size(64, 16);
this.label3.TabIndex = 9;
this.label3.Text = "数据长度:";
//
// checkBox1
//
this.checkBox1.Location = new System.Drawing.Point(88, 168);
this.checkBox1.Name = "checkBox1";
this.checkBox1.Size = new System.Drawing.Size(24, 24);
this.checkBox1.TabIndex = 11;
//
// btnAdd
//
this.btnAdd.Location = new System.Drawing.Point(16, 16);
this.btnAdd.Name = "btnAdd";
this.btnAdd.Size = new System.Drawing.Size(56, 32);
this.btnAdd.TabIndex = 12;
this.btnAdd.Text = "增加";
this.btnAdd.Click += new System.EventHandler(this.btnAdd_Click);
//
// btnSav
//
this.btnSav.Enabled = false;
this.btnSav.Location = new System.Drawing.Point(16, 160);
this.btnSav.Name = "btnSav";
this.btnSav.Size = new System.Drawing.Size(56, 32);
this.btnSav.TabIndex = 13;
this.btnSav.Text = "创建";
this.btnSav.Click += new System.EventHandler(this.btnSav_Click);
//
// label4
//
this.label4.Location = new System.Drawing.Point(8, 24);
this.label4.Name = "label4";
this.label4.Size = new System.Drawing.Size(64, 16);
this.label4.TabIndex = 14;
this.label4.Text = "表 名:";
//
// textBox3
//
this.textBox3.Location = new System.Drawing.Point(80, 16);
this.textBox3.Name = "textBox3";
this.textBox3.Size = new System.Drawing.Size(144, 21);
this.textBox3.TabIndex = 15;
this.textBox3.Text = "";
this.textBox3.TextChanged += new System.EventHandler(this.textBox3_TextChanged);
//
// groupBox1
//
this.groupBox1.Controls.Add(this.label5);
this.groupBox1.Controls.Add(this.label2);
this.groupBox1.Controls.Add(this.label4);
this.groupBox1.Controls.Add(this.textBox3);
this.groupBox1.Controls.Add(this.label1);
this.groupBox1.Controls.Add(this.textBox1);
this.groupBox1.Controls.Add(this.checkBox1);
this.groupBox1.Controls.Add(this.textBox2);
this.groupBox1.Controls.Add(this.label3);
this.groupBox1.Controls.Add(this.comboBox1);
this.groupBox1.Location = new System.Drawing.Point(8, 224);
this.groupBox1.Name = "groupBox1";
this.groupBox1.Size = new System.Drawing.Size(232, 200);
this.groupBox1.TabIndex = 16;
this.groupBox1.TabStop = false;
//
// label5
//
this.label5.Location = new System.Drawing.Point(8, 96);
this.label5.Name = "label5";
this.label5.Size = new System.Drawing.Size(64, 24);
this.label5.TabIndex = 17;
this.label5.Text = "数据类型:";
//
// label2
//
this.label2.Location = new System.Drawing.Point(16, 168);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(48, 24);
this.label2.TabIndex = 16;
this.label2.Text = "允许空";
//
// groupBox2
//
this.groupBox2.Controls.Add(this.btnDel);
this.groupBox2.Controls.Add(this.btnModify);
this.groupBox2.Controls.Add(this.btnAdd);
this.groupBox2.Controls.Add(this.btnSav);
this.groupBox2.Location = new System.Drawing.Point(272, 224);
this.groupBox2.Name = "groupBox2";
this.groupBox2.Size = new System.Drawing.Size(88, 200);
this.groupBox2.TabIndex = 17;
this.groupBox2.TabStop = false;
//
// CreateTable
//
this.AllowDrop = true;
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(368, 445);
this.Controls.Add(this.groupBox2);
this.Controls.Add(this.groupBox1);
this.Controls.Add(this.dataGrid1);
this.MaximizeBox = false;
this.MinimizeBox = false;
this.Name = "CreateTable";
this.Text = "创建新表";
this.Closing += new System.ComponentModel.CancelEventHandler(this.CreateTable_Closing);
this.Load += new System.EventHandler(this.CreateTable_Load);
((System.ComponentModel.ISupportInitialize)(this.dataGrid1)).EndInit();
this.groupBox1.ResumeLayout(false);
this.groupBox2.ResumeLayout(false);
this.ResumeLayout(false);
}
#endregion
#region //load
private void CreateTable_Load(object sender, System.EventArgs e)
{
this.comboBox1.SelectedIndex=0;
InitDataGrid();
}
#endregion
#region//初始dataGrid
private void InitDataGrid()
{
dt=new DataTable("InnitialDG");
string[] ArrstrColumn=new string[3]{"列名","数据类型","长度"};
for (int i=0;i<3;i++)
{
DataColumn dc=new DataColumn(ArrstrColumn[i]);
dc.DataType=Type.GetType("System.String");
dc.DefaultValue="";
dt.Columns.Add(dc);
}
//创建chengbox列
DataColumn dcCheckBox=new DataColumn("允许空");
dcCheckBox.DataType=Type.GetType("System.Boolean");
dcCheckBox.DefaultValue=false;
dt.Columns.Add(dcCheckBox);
dataGrid1.DataSource=dt;//绑定dataGrid
this.dataGrid1.Enabled=false;
}
#endregion
#region //选中即为选中一行
private void dataGrid1_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
{
}
private void dataGrid1_MouseUp(object sender, System.Windows.Forms.MouseEventArgs e)
{
if (e.Button==MouseButtons.Left)
{
System.Drawing.Point pt=new Point(e.X,e.Y);
DataGrid.HitTestInfo hti=dataGrid1.HitTest(pt);
if(hti.Type ==DataGrid.HitTestType.Cell)
{
dataGrid1.CurrentCell =new DataGridCell(hti.Row,hti.Column);
dataGrid1.Select(hti.Row);
}
int i=this.dataGrid1.CurrentRowIndex;
this.textBox1.Text=this.dataGrid1[i,0].ToString().Trim();
this.textBox2.Text=this.dataGrid1[i,2].ToString().Trim();
this.comboBox1.Text=this.dataGrid1[i,1].ToString().Trim();
this.checkBox1.Checked=Convert.ToBoolean(this.dataGrid1[i,3]);
}
}
#endregion
#region 根据数据类型的选择,确定数据长度;
private void comboBox1_SelectedIndexChanged(object sender, System.EventArgs e)
{
int i=comboBox1.SelectedIndex;
if (i==0)
{
this.textBox2.Clear();
this.textBox2.Text="4";
this.textBox2.ReadOnly=true;
}
if (i==1)
{
this.textBox2.Clear();
this.textBox2.Text="8";
this.textBox2.ReadOnly=true;
}
if (i==2)
{
this.textBox2.Clear();
this.textBox2.Text="50";
this.textBox2.ReadOnly=false;
}
}
#endregion
#region //转换允许空 为NULL or NOTNULL 用来添加到sql语句中
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -