📄 altertabel.cs
字号:
catch{}
}
object[] objArray = new object[hTable.Keys.Count ];
hTable.Keys.CopyTo(objArray,0);
return objArray;
}
#endregion
#endregion
#region //load
private void AlterTabel_Load(object sender, System.EventArgs e)
{
IsIdentityCol();
InitDataGrid(tablename);
drc=dt.Rows;
}
#endregion
#region 根据数据类型的选择,确定数据长度;
private void comboBox1_SelectedIndexChanged(object sender, System.EventArgs e)
{
string x=comboBox1.Text.ToString().Trim();
if (x.Equals("int"))
{
this.textBox3.Clear();
this.textBox3.Text="4";
this.textBox3.ReadOnly=true;//sql中要求int为4
}
if (x.Equals("float"))
{
this.textBox3.Clear();
this.textBox3.Text="8";
this.textBox3.ReadOnly=true;//sql中要求
}
if (x.Equals("nvarchar"))
{
this.textBox3.Clear();
this.textBox3.Text="50";
this.textBox3.ReadOnly=false;
}
}
#endregion
#region //判断那个是自动增长字段 dataGrid中隐藏
public void IsIdentityCol()
{
string sql=null;
// if (owner=="YuY")
// {
// sql="select name from syscolumns where columnproperty(id,name,'IsIdentity')=1 and id in(select id from sysobjects where name='[YuY]."+tablename+"')";
// }
// else
// {
sql="select name from syscolumns where columnproperty(id,name,'IsIdentity')=1 and id in(select id from sysobjects where name='"+tablename+"')";
// }
DataTable mydatatable=da.GetDataTable(sql);
IdentityCol=mydatatable.Rows[0][0].ToString().Trim();
}
#endregion
#region// 判断条件
public void Judge()
{
a=this.textBox1.Text.ToString().Trim();//表名
b=this.textBox2.Text.ToString().Trim();//列名
c=this.comboBox1.Text.Trim();//数据类型
d=this.textBox3.Text.ToString().Trim();//数据长度
if (a.Equals(""))
{
MessageBox.Show("表名不能为空");
return;
}
if (b.Equals(""))
{
MessageBox.Show("列名不能为空");
return;
}
if (c.Equals(""))
{
MessageBox.Show("请选择数据类型");
return;
}
if (d.Equals(""))
{
MessageBox.Show("请填写数据长度");
return;
}
}
#endregion
#region //dataGrid中 选中即为选中一行 并根据表的所有者判断能够执行删除修改命令
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;
if (i<dt1.Rows.Count-1)
{
this.textBox2.Text=this.dataGrid1[i,0].ToString();
this.textBox3.Text=this.dataGrid1[i,2].ToString();
this.comboBox1.Text=this.dataGrid1[i,1].ToString();
}
else
{
this.textBox2.Text=this.dataGrid1[i,0].ToString();
this.textBox3.Text=this.dataGrid1[i,2].ToString();
this.comboBox1.Text=this.dataGrid1[i,1].ToString();
this.btnDel.Enabled=true;
this.btnAlt.Enabled=true;
}
}
#endregion
#region//添加
private void btnAdd_Click(object sender, System.EventArgs e)
{
Judge();//条件判断
for (int K=0;K<dt1.Rows.Count-1;K++)//dataG中有数据
{
if (b.Equals(dataGrid1[K,0].ToString()))//判断列名是否重复
{
MessageBox.Show("该列名已经存在,请更改列名");
return;
}
}
// dataGrid1.Select(dataGrid1.VisibleRowCount);
string sql;
if (c=="int"|c=="float")//int float 不需要指定 长度
{
if (this.owner=="YuY")
{
sql="ALTER TABLE [YuY]."+tablename+" ADD "+b+" "+c+"" ;
}
else
{
sql="ALTER TABLE "+tablename+" ADD "+b+" "+c+"" ;
}
}
else
{
if (this.owner=="YuY")
{
sql="ALTER TABLE [YuY]."+tablename+" ADD "+b+" "+c+"( "+d+")" ;
}
else
{
sql="ALTER TABLE "+tablename+" ADD "+b+" "+c+"( "+d+")" ;
}
}
// MessageBox.Show(sql);
if (da.ExSQL(sql))
{
MessageBox.Show("添加成功");
CR=this.dataGrid1.CurrentRowIndex+1;
// DataRow myrow=dt.NewRow(); //dataTable添加方法
// myrow[0]=b;
// myrow[1]=c;
// myrow[2]=d;
// myrow[3]=this.checkBox1.Checked;
// drc.Add(myrow);
// dataGrid1.Refresh();
InitDataGrid2();
// this.dataGrid1.CurrentRowIndex=CR;
this.btnAlt.Enabled=true;
this.btnDel.Enabled=true;
}
else
{
MessageBox.Show("添加失败,请检查输入");
}
}
#endregion
#region//删除
private void btnDel_Click(object sender, System.EventArgs e)
{
if (this.dataGrid1.VisibleRowCount==0)
{
MessageBox.Show("请先添加数据");
}
if (this.dataGrid1.CurrentRowIndex<0)
{
MessageBox.Show("请先选中");
}
if (this.dataGrid1.VisibleRowCount==1)
{
if (MessageBox.Show("确定要删除该表吗?","重要提示",MessageBoxButtons.OKCancel)==DialogResult.OK)
{
string sql="drop table "+tablename+"";
if (da.ExSQL(sql))
{
this.Close();
}
return;
}
}
else
{
if (MessageBox.Show("确定要删除吗?","重要提示",MessageBoxButtons.OKCancel)==DialogResult.OK)
{
CR=this.dataGrid1.CurrentRowIndex-1;
int i =this.dataGrid1.CurrentRowIndex;
string col=this.dataGrid1[i,0].ToString().Trim();
string sql=null;
if (this.owner=="YuY")
{
sql="ALTER TABLE [YuY]."+tablename+" DROP COLUMN "+col+"" ;
}
else
{
sql="ALTER TABLE "+tablename+" DROP COLUMN "+col+"";
}
if(da.ExSQL(sql))
{
// DataColumnCollection dcc=dt.Columns;//取得table的列
// DataColumn dc=dcc[0];//第一列
// DataColumn[] mydc={dc};//转换为可用格式
// dt.PrimaryKey=mydc;//主键
// string s=dataGrid1[dataGrid1.CurrentRowIndex,0].ToString(); //dataGrid的列序号也就是主键的值
// DataRow myrow= dt.Rows.Find(s); //找到对应的行
// myrow.Delete();//标记为删除
// dataGrid1.Refresh();//更新dataGrid1
InitDataGrid2();
}
else
{
MessageBox.Show("删除失败");
}
}
else
{
return;
}
}
}
#endregion
#region //修改
private void btnAlt_Click(object sender, System.EventArgs e)
{
if (this.dataGrid1.VisibleRowCount==0)
{
MessageBox.Show("请先添加数据");
}
if (this.dataGrid1.CurrentRowIndex<0)
{
MessageBox.Show("请先选中");
}
Judge();
int i=dataGrid1.CurrentRowIndex;
string x=this.dataGrid1[i,0].ToString().Trim();
// foreach(DataRow dr in dt.Rows)
// {
// if (dr[0]==dataGrid1[i,0])
// {
// MessageBox.Show(dr[3].ToString());
//
// dr[0]=b;
// dr[1]=c;
// dr[2]=Convert.ToInt16(d);
// dr[3]=this.checkBox1.Checked.ToString();
if (MessageBox.Show("确定要修改吗","重要提示",MessageBoxButtons.OKCancel)==DialogResult.OK)
{
// dt.AcceptChanges();
// dataGrid1.Refresh();
string sql=null;
if (c=="int"|c=="float")//int float 不需要指定 长度
{
if (this.owner=="YuY")
{
sql="ALTER TABLE [YuY]."+tablename+" ALTER COLUMN "+x+" "+c+"" ;
}
else
{
sql="ALTER TABLE "+tablename+" ALTER COLUMN "+x+" "+c+"" ;
}
}
else
{
if (this.owner=="YuY")
{
sql="ALTER TABLE [YuY]."+tablename+" ALTER COLUMN "+x+" "+c+"( "+d+")" ;
}
else
{
sql="ALTER TABLE "+tablename+" ALTER COLUMN "+x+" "+c+"( "+d+")" ;
}
}
// MessageBox.Show(sql);
if (b!=x)
{
MessageBox.Show("列名不可以更改!");
return;
}
else
{
if (da.ExSQL(sql))
{
this.dataGrid1[i,1]=c;
this.dataGrid1[i,2]=d;
return;
}
else
{
MessageBox.Show("修改失败,请检查输入是否合法");
}
}
}
else
{
return;
}
// }
// }
}
#endregion
private void dataGrid1_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
{
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -