我正在使用 SSIS 2019 并将 SSMS 作为我的数据库。我用 C# 在 SSIS 中编写了一个脚本,但由于某种原因它无法连接到我的数据库。 我设法查看有问题的行及其带有 ** 的行(请参阅代码部分)。无论如何,这条线只会给我带来空值。我给这段代码的变量是正确的,我仔细检查过。它是我想念的小东西。
如果你有任何见解,我会很高兴听到。
引发的错误:
SSIS package "C:\Users\shale\source\repos\Integration Services Project3\Integration
Services Project3\CompareCoulmnsNames.dtsx" starting.
Error: 0x0 at Script Task, Exception from Script Task: ExecuteNonQuery: Connection
property has not been initialized.
at System.Data.SqlClient.SqlCommand.ValidateCommand(String method, Boolean async)
at System.Data.SqlClient.SqlCommand.InternalExecuteNonQuery(TaskCompletionSource`1
completion, String methodName, Boolean sendToPipe, Int32 timeout, Boolean& usedCache,
Boolean asyncWrite, Boolean inRetry)
at System.Data.SqlClient.SqlCommand.ExecuteNonQuery()
at ST_0802b46cfde44ff192edd969586bd834.ScriptMain.Main()
Warning: 0x80019002 at Script Task: SSIS Warning Code DTS_W_MAXIMUMERRORCOUNTREACHED.
The Execution method succeeded, but the number of errors raised (1) reached the maximum
allowed (1); resulting in failure. This occurs when the number of errors reaches the
number specified in MaximumErrorCount. Change the MaximumErrorCount or fix the errors.
Task failed: Script Task
Warning: 0x80019002 at Foreach Loop Container: SSIS Warning Code
DTS_W_MAXIMUMERRORCOUNTREACHED. The Execution method succeeded, but the number of
errors raised (1) reached the maximum allowed (1); resulting in failure. This occurs
when the number of errors reaches the number specified in MaximumErrorCount. Change the
MaximumErrorCount or fix the errors.
Warning: 0x80019002 at Package1: SSIS Warning Code DTS_W_MAXIMUMERRORCOUNTREACHED. The
Execution method succeeded, but the number of errors raised (1) reached the maximum
allowed (1); resulting in failure. This occurs when the number of errors reaches the
number specified in MaximumErrorCount. Change the MaximumErrorCount or fix the errors.
SSIS package "C:\Users\shale\source\repos\Integration Services Project3\Integration
Services Project3\CompareCoulmnsNames.dtsx" finished: Failure.
The program '[3020] DtsDebugHost.exe: DTS' has exited with code 0 (0x0).
代码如下:
public void Main()
{
string strPath1 = Dts.Variables["File1_PATH"].Value.ToString();
if (File.Exists(strPath1))
{
string[] file1 = File.ReadAllLines(strPath1);
Dts.Variables["COLUMN_NAMES"].Value = file1[0];
}
string strArr1 = Dts.Variables["COLUMN_NAMES"].Value.ToString();
string strPath2 = Dts.Variables["FilesPath"].Value.ToString();
string[] strArr2 = File.ReadAllLines(strPath2);
DateTime loadingDateTime = new DateTime();
string fileName = Path.GetFileName(strPath2);
int records = 0;
string status = "";
if (Enumerable.SequenceEqual(strArr1, strArr2[0]))
{
Dts.Variables["BooleanVar"].Value = true;
try
{
int counter = 0;
string TableName = Dts.Variables["TABLE_NAME"].Value.ToString();
**SqlConnection conn = (SqlConnection)(Dts.Connections[Dts.Variables["CONNECTION_MANAGER"].Value.ToString()].AcquireConnection(Dts.Transaction) as SqlConnection);**
// Check the connection state
if (conn == null) { MessageBox.Show("null"); }
foreach (string i in strArr2)
{
if (counter != 0)
{
string query = "Insert into " + TableName + " values('" + i.Replace(",", "','").Replace(" ", "','") + "')";
SqlCommand SQLCommand = new SqlCommand(query, conn);
SQLCommand.ExecuteNonQuery();
}
counter++;
}
//change the current file data
loadingDateTime = DateTime.Now;
records = counter - 1;
status = "Processed";
Dts.TaskResult = (int)ScriptResults.Success;
conn.Close();
}
catch (Exception ex)
{
Dts.Events.FireError(0, "Exception from Script Task", ex.Message + "\r" + ex.StackTrace, String.Empty, 0);
Dts.TaskResult = (int)ScriptResults.Failure;
}
}
else
{
Dts.Variables["BooleanVar"].Value = false;
status = "Wrong";
}
//insert the file data to etl_report
string dest = Dts.Variables["DEST_PATH"].Value.ToString();
string data = fileName + "," + status + "," + records.ToString() + "," + loadingDateTime.ToString("dd/MM/yyyy hh:mm tt");
if (!File.Exists(dest))
{
string clientHeader = "Filename" + "," + "Status" + "," + "Records" + "," + "LoadingDateTime" + Environment.NewLine;
File.WriteAllText(dest, clientHeader);
}
File.AppendAllText(dest, data + Environment.NewLine);
Dts.TaskResult = (int)ScriptResults.Success;
}
你必须使用这个代码 我的代码(正确代码):
SqlConnection conn = new SqlConnection(Your connectionstring);
//example connectionstring :"Password=123;Persist Security Info=True;User ID=sad;Initial Catalog=TestDB1;Data Source=.;TrustServerCertificate=True"
// Check the connection state
if (conn.State == ConnectionState.Closed) conn.Open();
你的代码
string TableName = Dts.Variables["TABLE_NAME"].Value.ToString();
**SqlConnection conn = (SqlConnection)(Dts.Connections[Dts.Variables["CONNECTION_MANAGER"].Value.ToString()].AcquireConnection(Dts.Transaction) as SqlConnection);**
// Check the connection state
if (conn == null) { MessageBox.Show("null"); }