publicstaticArrayList GetSqlFile(stringvarFileName,stringdbname)
{
ArrayList alSql=newArrayList();if(!File.Exists(varFileName))
{returnalSql;
}
StreamReader rs=newStreamReader(varFileName, System.Text.Encoding.Default);//注意编码stringcommandText="";stringvarLine="";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{
alSql.Add(commandText);
commandText="";
}
}
rs.Close();returnalSql;
}publicstaticvoidExecuteCommand(ArrayList varSqlList,stringconnString)
{
SqlConnection MyConnection=newSqlConnection(connString);
MyConnection.Open();
SqlTransaction varTrans=MyConnection.BeginTransaction();
SqlCommand command=newSqlCommand();
command.Connection=MyConnection;
command.Transaction=varTrans;try{foreach(stringvarcommandTextinvarSqlList)
{
command.CommandText=varcommandText;
command.ExecuteNonQuery();
}
varTrans.Commit();
}catch(Exception ex)
{
varTrans.Rollback();throwex;
}finally{
MyConnection.Close();
}
}