C#和Java中执行SQL文件脚本的代码(非常有用)

我们在做程序的时候有事后会涉及到利用sql文件 直接执行,可是在sql文件中有很多注释,我们要一句一句的执行首先必须的得把sql文件解析
去除其中的注释,还有把每一句sql语句取出来,然后再利用各个平台中的数据库相关执行它。
接下来放代码!
java版本的
004 | import java.util.ArrayList;
|
005 | import java.util.Enumeration;
|
006 | import java.util.List;
|
007 | import java.util.Vector;
|
136 | if (!mm.trim().equals(""))
|
140 | else if (str.indexOf("//")>=0)
|
142 | tm=str.substring(0,str.indexOf("//"));
|
143 | if (!tm.trim().equals(""))
|
152 | File fName=new File(fileStr);
|
153 | FileWriter in=new FileWriter(fName);
|
155 | Enumeration<String> ew=vec.elements();
|
157 | while (ew.hasMoreElements()) {
|
158 | ssss= ew.nextElement().toString();
|
165 | }catch(Exception ee){
|
166 | ee.printStackTrace();
|
调用GetText就可以返回一个装满了sql语句的数组,循环执行其中的sql语句吧
c#版本的
003 | /// 获取sql文件中的sql语句数组 第一种方法
|
005 | /// <param name="sql"></param>
|
006 | /// <returns></returns>
|
007 | public static string[] sql_split(string sql)
|
010 | Regex reg = newRegex("/TYPE=(InnoDB|MyISAM|MEMORY)( DEFAULT CHARSET=[^; ]+)?/");
|
011 | reg.Replace(sql, "ENGINE=\\1 DEFAULT CHARSET=utf8");
|
012 | s = s.Replace('\r', '\n');
|
013 | string[] ret = new string[10000];
|
014 | string[] sqlarray = StringSplit(s, ";\n");
|
016 | foreach (string item in sqlarray)
|
019 | string[] queries = item.Split('\n');
|
020 | queries = filter(queries);
|
021 | foreach (string query in queries)
|
023 | string str1 = query.Substring(0, 1);
|
024 | string str2 = query.Substring(0, 2);
|
025 | if (str1 != "#" && str2 != "--" && str2 != "/*"&& str2 != "//")//去除注释的关键步奏
|
039 | /// <param name="ss"></param>
|
040 | /// <returns></returns>
|
041 | public static string[] filter(string[] ss)
|
043 | List<string> strs = new List<string>();
|
044 | foreach (string s in ss)
|
046 | if (!string.IsNullOrEmpty(s)) strs.Add(s);
|
048 | string[] result = strs.ToArray();
|
054 | /// <param name="strSource"></param>
|
055 | /// <param name="strSplit"></param>
|
056 | /// <returns></returns>
|
057 | public static string[] StringSplit(string strSource, string strSplit)
|
059 | string[] strtmp = new string[1];
|
060 | int index = strSource.IndexOf(strSplit, 0);
|
063 | strtmp[0] = strSource;
|
068 | strtmp[0] = strSource.Substring(0, index);
|
069 | returnStringSplit(strSource.Substring(index + strSplit.Length), strSplit, strtmp);
|
076 | /// <param name="strSource"></param>
|
077 | /// <param name="strSplit"></param>
|
078 | /// <param name="attachArray"></param>
|
079 | /// <returns></returns>
|
080 | private static string[] StringSplit(string strSource, stringstrSplit, string[] attachArray)
|
082 | string[] strtmp = new string[attachArray.Length + 1];
|
083 | attachArray.CopyTo(strtmp, 0);
|
085 | int index = strSource.IndexOf(strSplit, 0);
|
088 | strtmp[attachArray.Length] = strSource;
|
093 | strtmp[attachArray.Length] = strSource.Substring(0, index);
|
094 | returnStringSplit(strSource.Substring(index + strSplit.Length), strSplit, strtmp);
|
102 | /// 获取sql文件中的sql语句数组 第二种
|
104 | /// <param name="sql"></param>
|
105 | /// <returns></returns>
|
106 | public string[] getsqls(string sql)
|
109 | s = s.Replace("\r\n", "\n");
|
110 | s = s.Replace("\r","\n").Trim();
|
111 | string[] ret = new string[1000];
|
113 | string[] sqlarray= StringSplit(s, ";\n");
|
114 | sqlarray = filter(sqlarray);
|
117 | foreach (string item in sqlarray)
|
119 | string ret_item = "";
|
120 | string[] querys = item.Trim().Split('\n');
|
121 | querys = filter(querys);
|
123 | foreach (string query in querys)
|
125 | string str1 = query.Substring(0, 1);
|
126 | string str2 = query.Substring(0, 2);
|
127 | if (str1 == "#" || str2 == "--" || str2 == "/*"|| str2 == "//")//去除注释的关键步奏
|
c#两个方法对sql文件解析都是一样的
以上是 C#和Java中执行SQL文件脚本的代码(非常有用) 的全部内容,
来源链接:
utcz.com/z/394507.html