private void metroButton2_Click(object sender, EventArgs e)
{
string[] contens = new string[0];
string FileNameSql = "";
string templateFile = AppDomain.CurrentDomain.BaseDirectory;//程序文件目录
bool file_b = false;//默认txt文件不存在
if (Directory.Exists(templateFile))//若文件夹存在
{
FileNameSql= templateFile + "\\创建档案条目表.txt";
file_b = File.Exists(templateFile + "\\创建档案条目表.txt");//文件是否存在
if (file_b)
{
contens = File.ReadAllLines(templateFile + "\\创建档案条目表.txt");//存在读取这个txt
}
}
//获取到数据库连接信息
ExecuteCommand(GetSqlFile(FileNameSql, ""),DaoFactory.NewSJDao(Common.g_DataSource));
MessageBox.Show("创建案卷表和卷内表成功!");
}
/// 读取文件并且修改
/// </summary>
/// <param name="varFileName">文件路径</param>
/// <param name="dbname">数据库表名</param>
/// <returns></returns>
public ArrayList GetSqlFile(string varFileName, string dbname)
{
ArrayList alSql = new ArrayList();
if (!File.Exists(varFileName))
{
return alSql;
}
StreamReader rs = new StreamReader(varFileName, System.Text.Encoding.Default);//注意编码
string commandText = "";
string varLine = "";
while (rs.Peek() > -1)
{
varLine = rs.ReadLine();
if (varLine == "")
{
continue;
}
if (varLine != "GO" && varLine != "go")
{
commandText += varLine;
commandText = commandText.Replace("@database_name=N\'dbhr\'", string.Format("@database_name=N\'{0}\'", dbname));
commandText += "\r\n";
}
else
{
//将@ArchvTable和@ArchvFileTable替换成案卷表名和卷内表名
string pcom = "";
if(commandText.Contains("@ArchvTable")|| commandText.Contains("@ArchvFileTable"))
{
pcom= commandText.Replace("@ArchvTable", Common.g_QXPZAJ);
pcom = pcom.Replace("@ArchvFileTable", Common.g_QXPZJN);
}
alSql.Add(pcom);
commandText = "";
}
}
rs.Close();
return alSql;
}
/// 连接数据库表并执行文件中的sql语句
/// </summary>
/// <param name="varSqlList"></param>
/// <param name="connString"></param>
public static void ExecuteCommand(ArrayList varSqlList, string connString)
{
SqlConnection MyConnection = new SqlConnection(connString);
MyConnection.Open();
SqlTransaction varTrans = MyConnection.BeginTransaction();
SqlCommand command = new SqlCommand();
command.Connection = MyConnection;
command.Transaction = varTrans;
try
{
{
command.CommandText = varcommandText;
command.ExecuteNonQuery();
}
varTrans.Commit();
}
catch (Exception ex)
{
varTrans.Rollback();
throw ex;
}
finally
{
MyConnection.Close();
}
}