Torna al Thread
try
{
String wholeSql = String.Empty;
String fname = @"C:\SQLScript\kkk.sql";
if (File.Exists(fname))
{
using (StreamReader sr = new StreamReader(fname))
{
wholeSql = sr.ReadToEnd();
}
}
//Eseguo lo split su ogni riga che inizia con GO
Regex regEx = new Regex("^GO", RegexOptions.Multiline);
String[] sqlStatements = regEx.Split(wholeSql);
List<String> ret = new List<String>();
foreach (String s in sqlStatements)
{
if (!(s.StartsWith("USE", StringComparison.OrdinalIgnoreCase) && s.Trim().Length > 0))
{
ret.Add(s.Trim());
}
}
using (TransactionScope ts = new TransactionScope(TransactionScopeOption.Required,new TimeSpan(0,5,0)))
using (SqlConnection conn = new SqlConnection("Data Source=SOURCE;Initial Catalog=MIODB;Persist Security Info=True;User ID=sa;Password=pwd"))
{
SqlCommand cmd = new SqlCommand();
cmd.Connection = conn;
//loop through each block in the spilt SQL and execute.
foreach (String sql in ret)
{
if (sql.Trim().Length > 0)
{
using (TransactionScope ts1 = new TransactionScope(
TransactionScopeOption.Required, new TimeSpan(0, 5, 0)))
{
cmd.CommandText = sql;
conn.Open();
cmd.ExecuteNonQuery();
conn.Close();
ts1.Complete();
}
}
}
ts.Complete();
}
}
catch (Exception ex)
{
throw ex;
}