📄 frmsqlscript.cs
字号:
public void OpenFile()
{
System.Text.StringBuilder strBld = new System.Text.StringBuilder();
OpenFileDialog openFileDialog1 = new OpenFileDialog();
openFileDialog1.InitialDirectory = this.FilePath;
openFileDialog1.Filter = "SQL文件|*.sql";
if(openFileDialog1.ShowDialog() == DialogResult.OK)
{
this.TxtBoxSql.Clear();
this.keyWordColorOn = false;
System.Timers.Timer time = new System.Timers.Timer();
time.Interval = 300;
//this.ultraStatusBar1.Panels[0].Visible = true;
try
{
FileStream fs = new FileStream(openFileDialog1.FileName,FileMode.Open,FileAccess.Read);
StreamReader m_streamReader = new StreamReader(fs) ;
//使用StreamReader类来读取文件
m_streamReader.BaseStream.Seek(0,SeekOrigin.Begin) ;
// 从数据流中读取每一行,直到文件的最后一行
string strLine = m_streamReader.ReadLine();
while(strLine!=null)
{
time.Enabled = true;
strBld.Append(strLine+"\n");
strLine = m_streamReader.ReadLine();
}
//关闭此StreamReader对象
m_streamReader.Close() ;
this.TxtBoxSql.Text = strBld.ToString();
}
catch( Exception em )
{
this.TxtBoxSql.Text = em.Message.ToString();
}
}
}
public void OpenFileToBox()
{
OpenFileDialog openFileDialog1 = new OpenFileDialog();
openFileDialog1.InitialDirectory = this.FilePath;
openFileDialog1.Filter = "SQL文件|*.sql";
if(openFileDialog1.ShowDialog() == DialogResult.OK)
{
this.TxtBoxSql.Clear();
try
{
this.TxtBoxSql.LoadFile(openFileDialog1.FileName,RichTextBoxStreamType.PlainText);
}
catch( Exception em )
{
this.txtResult.Text = em.Message.ToString();
}
}
}
public void SaveFile()
{
SaveFileDialog saveFileDialog = new SaveFileDialog();
try
{
//获得另存为的文件名称
if( saveFileDialog.ShowDialog() == DialogResult.OK )
{
//创建一个文件流,用以写入或者创建一个StreamWriter
FileStream fs = new FileStream(@saveFileDialog.FileName,FileMode.OpenOrCreate,FileAccess.Write) ;
StreamWriter m_streamWriter = new StreamWriter(fs) ;
try
{
m_streamWriter.Flush() ;
// 使用StreamWriter来往文件中写入内容
m_streamWriter.BaseStream.Seek(0, SeekOrigin.Begin) ;
// 把对象中的内容写入文件
m_streamWriter.Write(this.TxtBoxSql.Text) ;
//关闭此文件
m_streamWriter.Flush() ;
m_streamWriter.Close() ;
fs.Close();
GC.Collect();
}
catch{GC.Collect();}
}
}
catch(Exception em)
{
throw em;
}
}
public void SaveFile(string fileName)
{
try
{
FileStream fs = new FileStream(fileName, FileMode.OpenOrCreate, FileAccess.Write);
StreamWriter m_streamWriter = new StreamWriter(fs);
m_streamWriter.Flush();
// 使用StreamWriter来往文件中写入内容
m_streamWriter.BaseStream.Seek(0, SeekOrigin.Begin);
// 把对象中的内容写入文件
m_streamWriter.Write(this.TxtBoxSql.Text);
//关闭此文件
m_streamWriter.Flush();
m_streamWriter.Close();
fs.Close();
}
catch(Exception em)
{
throw em;
}
}
public void ClearFile(string fileName)
{
try
{
FileStream fs = new FileStream(fileName, FileMode.OpenOrCreate, FileAccess.Write);
fs.SetLength(0);
fs.Close();
GC.Collect();
}
catch
{
GC.Collect();
}
}
#endregion
#region TimePragrass
public void Init()
{
this.timer = new System.Threading.Timer(new System.Threading.TimerCallback(ShowProgress),null,-1,-1);
}
public void ShowProgress(object obj)
{
if(this.InvokeRequired)
{
object[] pList = {obj};
BeginInvoke( new MyProgressEventsHandler(UpdateUI),pList);
}
else
{
UpdateUI(obj);
}
}
private void UpdateUI(object obj)
{
Application.DoEvents();
if(this.ultraStatusBar1.Panels[0].ProgressBarInfo.Value>=100)
this.ultraStatusBar1.Panels[0].ProgressBarInfo.Value = 0;
this.ultraStatusBar1.Panels[0].ProgressBarInfo.Value ++;
}
private delegate void MyProgressEventsHandler(object obj) ;
#endregion
#region ExecuteSql
public void ExecuteSql()
{
this.ultraStatusBar1.Panels[0].Visible = true;
string path = AppDomain.CurrentDomain.SetupInformation.ApplicationBase;
path = path+@"Resources\SCRIPT\tmp.sql";
this.timer.Change(0,50);
try
{
ClearFile(path);
SaveFile(path);
SQLPlusScript sql = new SQLPlusScript();
this.txtResult.Text = sql.Execute(path);
}
catch(Exception ex)
{
this.txtResult.Text = ex.Message.ToString();
ClearFile(path);
}
finally
{
this.timer.Change(-1,-1);
this.ultraStatusBar1.Panels[0].ProgressBarInfo.Value = 0;
this.ultraStatusBar1.Panels[0].Visible = false;
}
}
#endregion
public void SetWhileRead(bool on)
{
if(on)
{
this.TxtBoxSql.ReadOnly = on;
this.Cursor = System.Windows.Forms.Cursors.WaitCursor;
}
else
{
this.TxtBoxSql.ReadOnly = on;
this.Cursor = System.Windows.Forms.Cursors.Default;
}
}
#endregion
#region ### 自定义事件 ###
public delegate string LoadFileToTextDelegate(OpenFileDialog dialog);
#endregion
#region ### 控件事件方法 ###
private void FrmSqlScript_Load(object sender, System.EventArgs e)
{
}
private void TxtBoxSql_TextChanged(object sender, System.EventArgs e)
{
if(this.KeyWordColorOn)
{
int index = this.TxtBoxSql.SelectionStart; //记录修改的位置
this.TxtBoxSql.SelectAll();
this.TxtBoxSql.SelectionColor = Color.Black;
for (int i = 0; i < keystr.Length; i++)
this.getbunch(keystr[i], this.TxtBoxSql.Text);
this.TxtBoxSql.Select(index, 0); //返回修改的位置
this.TxtBoxSql.SelectionColor = Color.Black;
}
}
private void MainUltraToolbarsManager_ToolClick(object sender, DS.Win.UltraWinToolbars.ToolClickEventArgs e)
{
switch(e.Tool.Key.ToUpper())
{
case "OPEN":
{
OpenFileToBox();
break;
}
case "EXIT":
{
this.Close();
break;
}
case "CHECK" :
{
break;
}
case "EXECUTE" :
{
this.txtResult.Clear();
ExecuteSql();
break;
}
case "SAVE" :
{
SaveFile();
break;
}
default :
break;
}
}
#endregion
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -