public
static
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
{
alSql.Add(commandText);
commandText = "" ;
}
}
rs.Close();
return alSql;
}
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
{
foreach ( string varcommandText in varSqlList)
{
command.CommandText = varcommandText;
command.ExecuteNonQuery();
}
varTrans.Commit();
}
catch (Exception ex)
{
varTrans.Rollback();
throw ex;
}
finally
{
MyConnection.Close();
}
}
{
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
{
alSql.Add(commandText);
commandText = "" ;
}
}
rs.Close();
return alSql;
}
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
{
foreach ( string varcommandText in varSqlList)
{
command.CommandText = varcommandText;
command.ExecuteNonQuery();
}
varTrans.Commit();
}
catch (Exception ex)
{
varTrans.Rollback();
throw ex;
}
finally
{
MyConnection.Close();
}
}