using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace TableUpdator
{
public partial class FormTableUpdator : Form
{
public FormTableUpdator()
{
InitializeComponent();
}
int FiledStep = 1;
private void buttonSplit_Click(object sender, EventArgs e)
{
DataTable dt = new DataTable();
dt.Columns.Add("ColumnName", typeof(string));
dt.Columns.Add("ColumnDescrption", typeof(string));
dt.Columns.Add("ColumnType", typeof(string));
dt.Columns.Add("ColumnDefault", typeof(string));
string ColumnName = string.Empty;
string ColumnDescrption = string.Empty;
string ColumnType = string.Empty;
string mFiled = this.richTextBoxFiledText.Text.Trim();
string mSplit = this.textBoxSplit.Text.Trim();
if (string.IsNullOrWhiteSpace(mFiled))
{
MessageBox.Show("字段串不能为空!");
return;
}
this.richTextBox2.Text = string.Empty;
if (mFiled.Contains(mSplit))
{
string[] FiledItems = mFiled.Split(mSplit[0]);
foreach (string text in FiledItems)
{
ColumnDescrption = text.Trim().TrimEnd(mSplit[0]);
this.richTextBox2.Text += ColumnDescrption + "," + SpellHelper.GetSpellCode(ColumnDescrption).ToLower() + FiledStep.ToString() + "\n";
DataRow dr = dt.NewRow();
dr["ColumnName"] = SpellHelper.GetSpellCode(ColumnDescrption).ToLower() + FiledStep.ToString();
dr["ColumnDescrption"] = ColumnDescrption;
if (ColumnDescrption.Contains("单位") || ColumnDescrption.Contains("人") || ColumnDescrption.Contains("类型") || ColumnDescrption.Contains("是否") || ColumnDescrption.Contains("方式"))
{
dr["ColumnType"] = "int";
}
else if (ColumnDescrption.Contains("时间"))
{
dr["ColumnType"] = "datetime";
}
else
{
dr["ColumnType"] = "nvarchar(50)";
}
dr["ColumnDefault"] = "";
dt.Rows.Add(dr);
FiledStep++;
}
}
else
{
ColumnDescrption = mFiled.Trim().TrimEnd(mSplit[0]);
this.richTextBox2.Text += ColumnDescrption + "," + SpellHelper.GetSpellCode(ColumnDescrption).ToLower() + FiledStep.ToString() + "\n";
DataRow dr = dt.NewRow();
dr["ColumnName"] = SpellHelper.GetSpellCode(ColumnDescrption).ToLower() + FiledStep.ToString();
dr["ColumnDescrption"] = ColumnDescrption;
if (ColumnDescrption.Contains("单位") || ColumnDescrption.Contains("人") || ColumnDescrption.Contains("类型") || ColumnDescrption.Contains("是否") || ColumnDescrption.Contains("方式"))
{
dr["ColumnType"] = "int";
}
else if (ColumnDescrption.Contains("时间"))
{
dr["ColumnType"] = "datetime";
}
else
{
dr["ColumnType"] = "nvarchar(50)";
}
dr["ColumnDefault"] = "";
dt.Rows.Add(dr);
}
//
dataGridViewFiles.DataSource = dt;
}
private void buttonBuild_Click(object sender, EventArgs e)
{
string ColumnName = string.Empty;
string ColumnDescrption = string.Empty;
string ColumnType = string.Empty;
string ColumnDefault = string.Empty;
string SqlDText = string.Empty;
string SqlText = string.Empty;
string SqlFText = string.Empty;
foreach (DataGridViewRow row1 in dataGridViewFiles.Rows)
{
ColumnName = row1.Cells["ColumnName"].Value.ToString();
ColumnDescrption = row1.Cells["ColumnDescrption"].Value.ToString();
ColumnType = row1.Cells["ColumnType"].Value.ToString();
ColumnDefault = row1.Cells["ColumnDefault"].Value.ToString();
SqlFText += string.Format("ALTER TABLE [dbo].[" + textBoxTName.Text + "] ADD [{0}] {1} NULL", ColumnName, ColumnType) + "\n GO \n";
SqlDText += string.Format("EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'" + ColumnDescrption + "' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'" + textBoxTName.Text + "', @level2type=N'COLUMN',@level2name=N'" + ColumnName + "'\n");
SqlDText += "GO\n";
}
SqlText += SqlFText.TrimEnd(',');
richTextBoxRS.Text = SqlText + SqlDText;
}
private void button1_Click(object sender, EventArgs e)
{
this.comboBoxTables.DisplayMember = "name";
this.comboBoxTables.ValueMember = "name";
this.comboBoxTables.DataSource = SqlHelper.ExecuteDataTable(this.textBoxDbStr.Text.Trim(), "select name from sys.sysobjects where xtype='u' order by name");
this.comboBoxTables.SelectedIndex = 0;
}
private void comboBoxTables_SelectedIndexChanged(object sender, EventArgs e)
{
FiledStep =(int) SqlHelper.ExecuteScalar(this.textBoxDbStr.Text.Trim(), @"SELECT COUNT(*)
FROM dbo.syscolumns a LEFT OUTER JOIN
dbo.systypes b ON a.xtype = b.xusertype INNER JOIN
dbo.sysobjects d ON a.id = d.id AND d.xtype = 'U' AND
d.status >= 0
where d.name='" +this.comboBoxTables.Text.ToString()+"'");
}
}
}