News

 

Working at a bank, I get to play with an iSeries a lot. I've been trying to build a data warehouse that uses day old data from the iseries and importing the data into a MS SQL database. I was able to do a data import using SSIS but I started to think about about how to do this in code. It was pretty easy!

First getting the data out of the iSeries/AS400. You have to install the latest IBM data provider. You get this when you install IBM's programmers toolkit. After you install the toolkit, add the reference to your project; IBM.Data.DB2.iSeries.

using IBM.Data.DB2.iSeries;

To read data out of IBM is almost the same as any other SQL statment with the expection of the table name. IBM uses libraries so your select statment has to have the library name in the from line - not a big deal;

The select statement is the same but your from statement has to point to the library and the table. The rest is the same, make a connection object, comman object and a data adapter.

string sql = "SELECT * FROM  LIBRARY.TABLE ";

            iDB2Connection conn = new iDB2Connection("DataSource=ibmserver.mydomain.com;userid=user;password=xxx");
            iDB2Command cmd = new iDB2Command(sql, conn);
            cmd.CommandType = System.Data.CommandType.Text;
            iDB2DataAdapter myCommand = new iDB2DataAdapter(cmd);
            myCommand.Fill(TempDS, "SQLStatement");
            TempDS.DataSetName = "SQLStatement";
           

Creating a table using SMO is pretty easy, You make a server object, a database object and a Table object. Then read through the columns to make the data types. In this example I make all of them VarChar(50). You should send the data column to an object and set the datatype in the object.

SqlConnection connection =  new SqlConnection(connectionString);
            Server server = new Server(new ServerConnection(connection));
            Database db = server.Databases["MyDataBase"];
            Table table = new Table(db, "MyTable");

            for (int i = 0; i < DT.Columns.Count; i++)
            {
                Column c = new Column(table, DT.Columns[i].ColumnName);
                c.DataType = DataType.VarChar(50);
                table.Columns.Add(c);
            }
            table.Create();

That is it - pretty simple. Now just read through your data table and insert the data to your new table.

 

  • Share This Post:
  • Share on Twitter
  • Share on Facebook
  • Share on Technorati

 

There are times you need to create different threads within your appliation. In this example you can create any amount of threads, queue them to start and they will close/open from the queue. This is how to get past the maxium of 25 threads at a time limit.

If you want a good example of the threads, open your Windows task manager, add Threads to your view and sort by threads while you run this test.

namespace ConsoleApplication1
{

 

class threading
{

 

static object worker = newobject();
statici nt runningworkers = 500;
public void Main()
{
for(inti = 0; i < runningworkers; i++)
{
ThreadPool.QueueUserWorkItem(RunThread, i);
}
 
lock(worker)
{
while(runningworkers > 0)
{
Monitor.Wait(worker);
}
}

 

Console.WriteLine("Complete...");
Console.ReadLine();
}

 

public static void RunThread(objectinstance)
{
Randomr = newRandom();
intsleep = r.Next(10000, 50000);
Thread.CurrentThread.Name = "Instance"+ instance;
Console.WriteLine("Started Instance: "+ instance + " Name: "+ Thread.CurrentThread.Name + " Managed ID: "+ Thread.CurrentThread.ManagedThreadId);
 
Thread.Sleep(sleep);
Console.WriteLine("Instance number "+ instance + " Ended after "+ sleep + " seconds"); 
lock(worker)
{
runningworkers--;
Monitor.Pulse(worker);
}

}

 

}

 

}

 


using System;
using System.Threading;
using System.Diagnostics;
  • Share This Post:
  • Share on Twitter
  • Share on Facebook
  • Share on Technorati

 

If you are like me it bugs you that users are forced to download images you placed in a html based email. Don't fret, you can embed the image and the user's will not be prompted to download images... 
 
Highlights:
 
HeaderInfo - needed for Outlook 2007; Not sure about 03...
 
AlternateView - Using a linked resource, this class embeds the linked resource in our case the image,
 
public boolSendEmailWithImage(stringMailToStr, stringMailFromStr, stringSubjectStr, stringBodyStr, stringImgPath)
{

string HeaderInfo = "<html xmlns:v=\"urn:schemas-microsoft-com:vml\" xmlns:o=\"urn:schemas-microsoft-com:office:office\" xmlns:w=\"urn:schemas-microsoft-com:office:word\" xmlns:m=\"http://schemas.microsoft.com/office/2004/12/omml\" xmlns=\"http://www.w3.org/TR/REC-html40\"><META HTTP-EQUIV=\"Content-Type\" CONTENT=\"text/html; charset=us-ascii\">";  

 

try
{
MailMessagemessage = newMailMessage(MailFromStr, MailToStr);
message.Subject = SubjectStr;
LinkedResource lr = newLinkedResource(ImgPath);
lr.ContentId = "Image"
AlternateView av = AlternateView.CreateAlternateViewFromString(HeaderInfo + BodyStr, null, MediaTypeNames.Text.Html);
av.TransferEncoding = TransferEncoding.QuotedPrintable;
av.LinkedResources.Add(lr);
message.AlternateViews.Add(av);
message.IsBodyHtml = true;
SmtpClientclient = newSmtpClient(ConfigurationManager.AppSettings["MailServerSetting"].ToString(), 25); 
client.UseDefaultCredentials = true;
client.Send(message);
 
return true;
}
catch(Exceptionex)
{
return false;
}
}
  • Share This Post:
  • Share on Twitter
  • Share on Facebook
  • Share on Technorati