懒得再单独写一个,所以就加了超级详细的注释
前几个判断主要是合并sql文件,什么样的sql对应的什么样的处理,
最后一个判断是合并任何文件
方法的两个参数:第一个是打开文件路径的集合,第二个参数文件保存路径
private void FileMerge(string[] filePath,string outFilePath) {
try
{
int b;
int n = filePath.Length;
//合并配置文件
//FileStream[] fileIn = new FileStream[n];
using (FileStream fileOut = new FileStream(outFilePath, FileMode.CreateNew)) //创建文件
{
string[] namezu = f1.hanshuName(); //声明一个存放存储过程名称的数组并赋值
foreach (string func in namezu) //循环数组
{
if (func != null & func != "")
{
string funcName = func;
//下面是拼接接口的配置信息
//MessageBox.Show(funcName);
string strSql1 = "exec proc_insert 'SYS_DEF_ASI_FUNC' ,'funcname=''" + funcName + "'' and COALESCE(inuse,0)=1 '";
DataSet ds1 = DM.ReturnTableEx(strSql1);
string txt1 = "";
string txt2 = "";
txt1 = ds1.Tables[0].Rows[0]["column1"].ToString();
string strSql2 = "exec proc_insert 'SYS_DEF_ASI_Func_PARAM' ,'funcname=''" + funcName + "'' and COALESCE(inuse,0)=1 '";
DataSet ds2 = DM.ReturnTableEx(strSql2);
foreach (DataRow dr2 in ds2.Tables[0].Rows)
{
string txt3 = "";
txt3 = dr2["column1"].ToString();
if (!string.IsNullOrEmpty(txt3))
{
txt2 = txt2 + "\r\n" + txt3;
}
}
string txt = "";
txt += "-----------------------------------" + funcName + "------------------------------\r\n";
if (!string.IsNullOrEmpty(txt1))
{
txt = txt + "\r\n" + " UPDATE SYS_DEF_ASI_FUNC SET INUSE=0 WHERE FUNCNAME='" + funcName + "' " + "\r\n"
+ txt1 + "\r\n";
}
if (!string.IsNullOrEmpty(txt2))
{
txt = txt + "\r\n" + " UPDATE SYS_DEF_ASI_Func_PARAM SET INUSE=0 WHERE FUNCNAME='" + funcName + "' " + "\r\n"
+ txt2 + "\r\n";
}
//拼接配置信息结束
StreamWriter sw = new StreamWriter(fileOut,Encoding.UTF8);
sw.Write(txt);//配置信息写入写入文本
sw.Flush();
}
}
//合并文件
for (int i = 0; i < n; i++)
{
if (filePath[i] == null)
{
break;
}
try
{
//分割
StreamReader st = new StreamReader(filePath[i], Encoding.Default); //获取文件
StreamWriter sw = new StreamWriter(fileOut, Encoding.UTF8); //声明写入
string name = st.ReadToEnd(); //读取文件
int CreateProc = name.IndexOf("PROCEDURE");
int AlterProc = name.IndexOf("PROCEDURE");
//判断是否是存储过程
if (CreateProc!=-1 || AlterProc!=-1){
name = name.Replace("ALTER", "CREATE"); //文本中存在ALTER那么就替换成CREATE
int a = name.IndexOf("-- Author"); //先找-- author
if (a == -1) //如果没有找到author
{
a = name.IndexOf("ALTER");
}
if (a == -1)
{
a = name.IndexOf("CREATE");
}
string file = name.Substring(a); // 作者之前的分割掉 截取作者之后的全部内容
int length = file.Length;
string GOFile = file.Substring(length-20,20);
int Goindex = GOFile.IndexOf("GO"); //判断文本最后有没有GO
if(Goindex==-1){
file += "\r\nGO\r\n"; //如果没有再文本的最后添加GO
}
int endindex = file.IndexOf("@"); //获取分割之后的文本中第一次出现@的下表:为分割存储过程名称的结束下表
int strtindex = file.IndexOf("PROCEDURE"); //获取分割之后的文本中第一次出现PROCEDURE的下表:为分割存储过程名称的开始下表
string filename = file.Substring(strtindex + 9, ((endindex - strtindex) -9)).Trim();//截取存储过程名称
int endindex1 = filename.IndexOf("\r\n"); //获取分割之后的文本中第一行
if (endindex1!=-1) //判断存储过程名称是否有好几行
{
filename = filename.Substring(0, endindex1).Trim();
}
filename=filename.Replace("[dbo].",""); //替换存储过程名称中的[dbo].
filename = filename.Replace("[", ""); //替换文本中的[
filename = filename.Replace("]", ""); //替换文本中的]
string txt = "\r\n-----------------------------" + filename + "存储过程-------------------------------------\r\n";//拼接一个存储过程的if判断,存在这个存储过程则删除
txt += "\r\nIF EXISTS(SELECT * FROM sysobjects WHERE type='p' AND NAME='" + filename + "')\r\nBEGIN\r\n DROP PROC " + filename + "\nEND\ngo";
file = txt + "\r\n" + "-- =============================================\r\n" + file;//存储过程的判断和分割后的文本进行拼接
sw.Write(file); //写入文本
sw.Flush();
}
int CreateView = name.IndexOf("VIEW");
int AlterView = name.IndexOf("VIEW");
//判断是否是视图(view)
if (CreateView != -1 || AlterView != -1)
{
int fileLength=name.Length; //获取文本的长度
string File = name;
File = File.Replace("ALTER", "CREATE"); //替换ALTER为CREATE
int ViewIndex = File.IndexOf("CREATE");
File = File.Substring(ViewIndex, ((fileLength - ViewIndex))).Trim(); //获取视图内容
int length = File.Length;
string GOFile = File.Substring(length - 20, 20);
int Goindex = GOFile.IndexOf("GO"); //判断文本最后有没有GO
if (Goindex == -1)
{
File += "\r\nGO\r\n"; //如果没有再文本的最后添加GO
}
int FileASLength = File.IndexOf("\n"); //换行的位置
int staIndex = File.IndexOf("CREATE");
string FileName = File.Substring(staIndex + 11, ((FileASLength - staIndex)-11)).Trim();//截取视图名称
FileName = FileName.Replace("[dbo].", ""); //替换视图名称中的[dbo].
FileName = FileName.Replace("[", ""); //替换文本中的[
FileName = FileName.Replace("]", ""); //替换文本中的]
string txt = "\r\n-----------------------------" + FileName + "视图-------------------------------------\r\n";//拼接一个视图的if判断,存在这个存储过程则删除
txt += "\r\nIF EXISTS (SELECT * FROM sys.views WHERE object_id = OBJECT_ID(N'" + FileName + "'))\r\n DROP VIEW " + FileName + "\r\nGO\r\n";
File = txt + "\r\n" + File;//视图的判断和分割后的文本进行拼接
sw.Write(File); //写入文本
sw.Flush();
}
//判断函数
int CreateFunction = name.IndexOf("FUNCTION");
int AlterFunction = name.IndexOf("FUNCTION");
if (CreateFunction != -1 || AlterFunction!=-1)
{
int fileLength = name.Length; //获取文本的长度
string File = name;
File = File.Replace("ALTER", "CREATE"); //替换ALTER为CREATE
int FUNCTIONIndex = File.IndexOf("CREATE");
File = File.Substring(FUNCTIONIndex, ((fileLength - FUNCTIONIndex))).Trim(); //获取函数内容
int length = File.Length;
string GOFile = File.Substring(length - 20, 20);
int Goindex = GOFile.IndexOf("GO"); //判断文本最后有没有GO
if (Goindex == -1)
{
File += "\r\nGO\r\n"; //如果没有再文本的最后添加GO
}
int FileASLength = File.IndexOf("\n"); //换行的位置
int staIndex = File.IndexOf("CREATE");
string FileName = File.Substring(staIndex + 16, ((FileASLength - staIndex) - 16)).Trim();//截取函数名称
int FileZGH = FileName.IndexOf("(");
if (FileZGH != -1) //判断函数名称是否有好几行
{
FileName = FileName.Substring(0, FileZGH).Trim();
}
if (FileZGH != -1) //判断函数名称是否有好几行
{
FileName = FileName.Substring(0, FileZGH).Trim();
}
FileName = FileName.Replace("[dbo].", ""); //替换函数名称中的[dbo].
FileName = FileName.Replace("[", ""); //替换文本中的[
FileName = FileName.Replace("]", ""); //替换文本中的]
string txt = "\r\n-----------------------------" + FileName + "函数-------------------------------------\r\n";//拼接一个函数的if判断,存在这个函数则删除
txt += "\nIF EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'" + FileName + "') AND type in (N'FN', N'IF', N'TF', N'FS', N'FT'))\r\n DROP FUNCTION " + FileName + "\r\nGO\r\n";
File = txt + "\r\n" + File;//函数的判断和分割后的文本进行拼接
sw.Write(File); //写入文本
sw.Flush();
}
if (CreateProc == -1 && CreateFunction == -1 && CreateView==-1)
{
string File = name;
string GOFile = File.Substring(File.Length - 20, 20);
int Goindex = GOFile.IndexOf("GO"); //判断文本最后有没有GO
if (Goindex == -1)
{
File += "\r\nGO\r\n"; //如果没有再文本的最后添加GO
}
int nameIndex = filePath[i].LastIndexOf(@"\");
string FileName = filePath[i].Substring(nameIndex+1,filePath[i].Length-nameIndex-1);
string txt = "\r\n--------------------------------------" + FileName + "--------------------------------------------\r\n";//拼接一个函数的if判断,存在这个函数则删除
File = txt + "\r\n" + File;//拼接
sw.Write(File); //写入文本
sw.Flush();
}
if (filePath.Length - 1 == i)
{
sw.Close(); //最后一次写完关闭
fileOut.Close();
}
//while ((b = fileIn[i].ReadByte()) != -1)
// fileOut.WriteByte((byte)b);
}
catch (System.Exception ex)
{
Console.WriteLine(ex.Message);
}
//finally
//{
// fileIn[i].Close();
//}
}
}
FN.MsgBox("提示信息", "生成成功。", 0 + 48);
}
catch (Exception ex)
{
//异常
return;
}
}