this example don't used paging but the logic is hidden somewhere there you just need to look of it.
As Akame will said. "Eliminated"
#region Namespaces
using System;
using System.Data;
using Microsoft.SqlServer.Dts. Runtime;
using System.Windows.Forms;
using Npgsql;
using System.Data.SqlClient;
#endregion
namespace ST_ 2e3c2d93319f4e9182d04dba0cbac2 75
{
/// <summary>
/// ScriptMain is the entry point class of the script. Do not change the name, attributes,
/// or parent of this class.
/// </summary>
[Microsoft.SqlServer.Dts. Tasks.ScriptTask. SSISScriptTaskEntryPointAttrib ute]
public partial class ScriptMain : Microsoft.SqlServer.Dts.Tasks. ScriptTask. VSTARTScriptObjectModelBase
{
Variables var = null;
public void Main()
{
Dts. VariableDispenser.LockForRead( "User::intNumberRecords");
Dts.VariableDispenser. LockForRead("User:: strPostGresSqlConn");
Dts.VariableDispenser. LockForRead("User:: SQLConnection");
Dts.VariableDispenser. LockForRead("User:: strSqlcmdBase");
Dts.VariableDispenser. LockForRead("User:: strSqlCmdLoop");
Dts.VariableDispenser. LockForRead("User:: strSqlCmdCount");
Dts.VariableDispenser. GetVariables(ref var);
Int64 TopNRecords = (Int64)var["User:: intNumberRecords"].Value;
String ConnectionString = var["User::strPostGresSqlConn" ].Value.ToString();
String SqlCmdBase = var["User::strSqlcmdBase"]. Value.ToString();
String SqlCmdLoop = var["User::strSqlCmdLoop"]. Value.ToString();
String SqlCmdCount = var["User::strSqlCmdCount"]. Value.ToString();
Int64 RecordCount = GetRecordCount( ConnectionString, SqlCmdCount);
Int64 paging = (int)Math.Ceiling((double) RecordCount / (double)TopNRecords);
bool isLoaded = ExtractData(ConnectionString, paging);
var.Unlock();
}
private Int64 GetRecordCount(String ConnectionString, String SqlCmd)
{
try
{
Int64 count = 0;
using (NpgsqlConnection conn = new NpgsqlConnection( ConnectionString))
{
conn.Open();
using (NpgsqlCommand cmd = new NpgsqlCommand(SqlCmd, conn))
{
count = (Int64)cmd.ExecuteScalar();
}
conn.Close();
}
return count;
}
catch (Exception e)
{
Dts.Events.FireError(0, "ErrorDescription", e.Message.ToString(), String.Empty, 0);
return 0;
}
}
private bool ExtractData(String ConnectionString, Int64 NumberPage)
{
bool IsSucess = false;
String SqlCmdTempate = var["User::strSqlCmdLoop"]. Value.ToString();
String SqlcmdBase = var["User::strSqlcmdBase"]. Value.ToString();
String SqlConn = var["User::SQLConnection"]. Value.ToString();
try
{
using (NpgsqlConnection conn = new NpgsqlConnection( ConnectionString))
{
conn.Open();
using (NpgsqlCommand cmd = new NpgsqlCommand(SqlcmdBase, conn))
{
using (NpgsqlDataReader reader = cmd.ExecuteReader())
else
dt = null;
}
conn.Close();
}
return IsSucess;
}
catch (Exception e)
{
String message = e.Message.ToString();
Dts.Events.FireError(0, "ErrorDescription", message, String.Empty, 0);
//raise error
return false;
}
}
private void LoadDataToSql(ref DataTable dt,String Destination,String ErrorDescription)
{
try
{
using (SqlConnection sqlcon = new SqlConnection( ErrorDescription))
{
sqlcon.Open();
CopyData(dt, sqlcon);
sqlcon.Close();
}
}
catch(Exception e)
{
Dts.Events.FireError(0, "ErrorDescription",e.Message. ToString(), String.Empty, 0);
}
}
static void CopyData(DataTable sourceTable, SqlConnection destConnection)
{
// new method: SQLBulkCopy:
using (SqlBulkCopy s = new SqlBulkCopy(destConnection))
{
s.DestinationTableName = "InventorySample";
s.ColumnMappings.Add("itemid", "itemid");
s.ColumnMappings.Add(" quantity", "quantity");
s.WriteToServer(sourceTable);
s.Close();
}
}
#region ScriptResults declaration
/// <summary>
/// This enum provides a convenient shorthand within the scope of this class for setting the
/// result of the script.
///
/// This code was generated automatically.
/// </summary>
enum ScriptResults
{
Success = Microsoft.SqlServer.Dts. Runtime.DTSExecResult.Success,
Failure = Microsoft.SqlServer.Dts. Runtime.DTSExecResult.Failure
};
#endregion
}
}
No comments:
Post a Comment