将错误捕获到 table 而不是文件 - C# 脚本任务 SSDT-2013
Capture error to table instead of file- C# script task SSDT-2013
谁能帮我修改下面的代码。我正在尝试捕获数据库 table 中的错误而不是文件中的错误。
当前代码正在将错误写入日志 table,我们如何修改以将相同的错误写入 table。
代码
catch (Exception exception)
{
// Create Log File for Errors
using (StreamWriter sw = File.AppendText(Dts.Variables["User::LogFolder"].Value.ToString()
+ "\" + "ErrorLog_" + datetime + ".log"))
{
sw.WriteLine(exception.ToString());
Dts.TaskResult = (int)ScriptResults.Failure;
}
}
Dts.TaskResult = (int)ScriptResults.Success;
}
如果可能,请分享您的宝贵建议。
谢谢....
首先,我会将 sql 对象创建移动到 try 之前,否则您将需要在 catch 块中重复它,这取决于您。
ConnectionManager ConMgr = Dts.Connections.Add("ADO.NET:SQL");
ConMgr.ConnectionString = "data source=database;initial catalog=zzzz_PRC;integrated security=true;persist security info=True;";
ConMgr.Name = "DBConn";
ConMgr.Description = "DBConn";
SqlConnection myADONETConnection = new SqlConnection();
myADONETConnection = (SqlConnection)
(Dts.Connections["DBConn"].AcquireConnection(Dts.Transaction) as SqlConnection);
接下来只是 运行 查询而不是记录到文件中:
catch (Exception exception)
{
string query = "Insert into " + SchemaName + ".logtable (Logtextcolumn) "; -- change tablename and columns desired.
query += "VALUES('" + exception.ToString() + "')";
SqlCommand myCommand1 = new SqlCommand(query, myADONETConnection);
myCommand1.ExecuteNonQuery();
Dts.TaskResult = (int)ScriptResults.Failure;
}
}
谁能帮我修改下面的代码。我正在尝试捕获数据库 table 中的错误而不是文件中的错误。
当前代码正在将错误写入日志 table,我们如何修改以将相同的错误写入 table。
代码
catch (Exception exception)
{
// Create Log File for Errors
using (StreamWriter sw = File.AppendText(Dts.Variables["User::LogFolder"].Value.ToString()
+ "\" + "ErrorLog_" + datetime + ".log"))
{
sw.WriteLine(exception.ToString());
Dts.TaskResult = (int)ScriptResults.Failure;
}
}
Dts.TaskResult = (int)ScriptResults.Success;
}
如果可能,请分享您的宝贵建议。
谢谢....
首先,我会将 sql 对象创建移动到 try 之前,否则您将需要在 catch 块中重复它,这取决于您。
ConnectionManager ConMgr = Dts.Connections.Add("ADO.NET:SQL");
ConMgr.ConnectionString = "data source=database;initial catalog=zzzz_PRC;integrated security=true;persist security info=True;";
ConMgr.Name = "DBConn";
ConMgr.Description = "DBConn";
SqlConnection myADONETConnection = new SqlConnection();
myADONETConnection = (SqlConnection)
(Dts.Connections["DBConn"].AcquireConnection(Dts.Transaction) as SqlConnection);
接下来只是 运行 查询而不是记录到文件中:
catch (Exception exception)
{
string query = "Insert into " + SchemaName + ".logtable (Logtextcolumn) "; -- change tablename and columns desired.
query += "VALUES('" + exception.ToString() + "')";
SqlCommand myCommand1 = new SqlCommand(query, myADONETConnection);
myCommand1.ExecuteNonQuery();
Dts.TaskResult = (int)ScriptResults.Failure;
}
}