Convert ImgContext Columns Spool Table of BizTalkMsgBoxDb Database into XML Format - c#

Is there any way that C# application Convert BizTalk Server BizTalkMsgBoxDb Database Spool Table ImgContext Columns in Original format that we show in BizTalk.
Here, Is my ImgContext value Datatype is image.
Here is my simple application code.
Here I need to actually output look like same as BizTalk.

Use the below code for resolved your logs issue with the external services, use the spool table of BizTalkMsgBoxDb.
if (row["imgContext"].GetType() != typeof(DBNull))
{
SqlBinary contextData = new SqlBinary((byte[])row["imgContext"])                         //Use memory stream and IBTMessageAgentFactory to get context
Console.WriteLine("Message Context:");
Console.WriteLine("");
MemoryStream contextstream = new MemoryStream(contextData.Value);
IBaseMessageContext context = ((IBTMessageAgentFactory)((IBTMessageAgent)new BTMessageAgent())).CreateMessageContext();
((IPersistStream) context).Load(contextstream); Console.WriteLine("NAME ,Value ,  TYPE , Namespace");
for (int i = 0; i<context.CountProperties; ++i)
{
string propName;
string propNamespace;
object propValue = context.ReadAt(i, out propName, out propNamespace);
string type = context.IsPromoted(propName, propNamespace) ? "Promoted" : "No Promoted";
Console.WriteLine(propName + " || " + propValue.ToString() + " || "+ type + " || "+ propNamespace );
}
}

Related

AWS Athena SDK with C#, how run DDL instructions?

I using the https://docs.aws.amazon.com/sdkfornet/v3/apidocs/items/Athena/NAthena.html sdk nuget https://github.com/aws/aws-sdk-net/ in order to query S3 bucket files, the thing is I need to create dataCatalog/databases and tables per demand (inside my program), I found a method to create dataCatalogs, but there is no such thing for databases or tables, only query methods. I'm confusing and want to know how can I do such thing.
Indeed there is no specific method for creating databases and tables, for that you will need to use QueryExecution requests
For databases:
var query = $"CREATE DATABASE IF NOT EXISTS DatabaseName " +
"COMMENT 'some comment' " +
"WITH DBPROPERTIES ('creator'='Me', 'Extra.'='some extra info');";
var request = new StartQueryExecutionRequest
{
QueryString = query,
ResultConfiguration = new ResultConfiguration
{
OutputLocation = _settings.DDLOutputLocation
}
};
For tables:
var query = "CREATE EXTERNAL TABLE IF NOT EXISTS " +
"TableName ( " +
"column1 int, " +
"column2 string, " +
"column3 int, " +
"column4 binary) " +
"ROW FORMAT SERDE 'org.apache.hadoop.hive.ql.io.parquet.serde.ParquetHiveSerDe' " +
$"LOCATION '{_settings.TableDataSourceLocation}'";
var request = new StartQueryExecutionRequest
{
QueryString = query,
QueryExecutionContext = new QueryExecutionContext
{
Catalog = _settings.CatalogName,
Database = _settings.DatabaseName
},
ResultConfiguration = new ResultConfiguration
{
OutputLocation = _settings.DDLOutputLocation
}
};

ASP.NET SQL Query One of Many Columns Undefined

General Description: Single SQL query of multiple SQL databases (through joins) returns all but one item from all but one database. Item not being returned is joined via an item that can, and sometimes is, null.
Specific Description:
I am continuing development of an internal ticketing system for work. I just started with C#, SQL and web development about a month ago, so I am still wrapping my head around all of the interconnections and syntax of Jquery, SQL, C#, and MVC.
Currently I am trying to display an SQL query in a table to show brief information for our tickets to support members. I have everything being displayed except "CircuitDescription" which is a pretty important element in order for support to differentiate between circuits. It is coming through on the table as "undefined" which I have gathered is the JQuery response for an initialized variable. All other values are coming through on the web page.
Running the SQL query through Microsoft SQL Server Management Studio displays the column with the circuit description.
Disclaimer:
While searching for this I have seen posts discouraging multiple database queries, but this is how the program was written before so I want to keep my code as similar to what has been done so far as possible.
So if we could jump past the part where it is implied I am an idiot (as is my only experience asking questions on stackoverflow) that would be lovely.
If you help me solve this, and help me learn why what you suggest works, then you can feel free to imply or directly call me anything you like.
Code:
C# / SQL Query:
-The join statements are combining the lists by a numeric value from DB "Tickets" with a numeric value from the other databases. Those databases are holding 2 columns, the numeric value and a corresponding string description.
-The full outer join is combining the list by a numeric circuitID from the tickets to a numeric circuitID in the circuits database.
-The circuit database holds the circuit description I am struggling with.
-Some circuitID values are null, which I suspect may be why this is not working. The other join statements that I am receiving data in connection to are all not null.
public static async Task<List<Ticket>> GetAllTicketsForCustomerAsync(DBAccess db, int customerID)
{
var cmd = "select TicketID, Tickets.DateCreated, Tickets.DateResolved, Tickets.CustomerCircuitID, CircuitDescription, TicketTypeDesc, BillingStatusDescription, TicketStatusDescription " +
"from Tickets " +
"join TicketTypes on Tickets.TicketTypeID = TicketTypes.TicketTypeID " +
"join TicketStatusTypes on Tickets.TicketStatus = TicketStatusTypes.TicketStatus " +
"join TicketBillingStatusTypes on Tickets.BillingStatus = TicketBillingStatusTypes.BillingStatus " +
"full outer join CustomerCircuits on Tickets.CustomerCircuitID = CustomerCircuits.CustomerCircuitID " +
"where Tickets.CustomerID = " + customerID +
"order by Tickets.TicketID DESC";
var table = await db.ReadTableAsync(cmd);
return (from DataRow row in table.Rows select db.AssignFromRow<Ticket>(row)).ToList();
}
JQuery:
-Ternary operator for circDesc is to list any tickets without a circuitID as "NonSpecific" for their circuit description. Otherwise they should display the circuit description that is currently coming through as "Undefined"
function buildPartialCustomerTicketsTable(tickets) {
var table = "";
var maxSize = (tickets.length < 5) ? tickets.length : 5;
for (var i = 0; i < maxSize; i++) {
var t = tickets[i];
var circDesc = (t.CustomerCircuitID == null) ? "Nonspecific" : t.CircuitDescription;
var rowClass = ((i % 2) == 0) ? "listRowNormal" : "listRowAlternate";
table += "<tr class='" + rowClass + "'>"
+ "<td class='listElement'><a href='#' onclick='viewTicket(" + t.TicketID + ",true)'>update</a></td>"
+ "<td class='listElement'>" + t.TicketStatusDescription + "</td>"
+ "<td class='listElement'>" + formatDate(t.DateCreated) + "</td>"
+ "<td class='listElement'>" + formatDate(t.DateResolved) + "</td>"
+ "<td class='listElement'>" + circDesc + "</td>"
+ "<td class='listElement'>" + t.TicketTypeDescription + "</td>"
+ "<td class='listElement'>" + t.BillingStatusDescription + "</td>"
+ "<td class='listElement'>" + t.TicketID + "</td>"
+ "</tr>";
}
return table;
}
Requested Code:
public T AssignFromRow<T>(DataRow row) where T : new()
{
var rec = new T();
this.AssignFromRow(row, rec);
return rec;
}
public void AssignFromRow(DataRow row, object rec)
{
if (row == null || rec == null)
{
return;
}
// Look at all of the properties in the record
PropertyInfo[] recprops = rec.GetType().GetProperties();
foreach (PropertyInfo pi in recprops)
{
// default the sql column name to the property name
string columnName = pi.Name;
// skip any read only parameters
if (!pi.CanWrite)
{
continue;
}
// Check for a mapping attribute. This attribute can change the name of the table column name from the default.
var customAttrs = pi.GetCustomAttributes(typeof(MappingAttribute), false);
if (customAttrs.Length > 0)
{
var mapping = (MappingAttribute)customAttrs[0];
if (!string.IsNullOrEmpty(mapping.ColumnName))
{
columnName = mapping.ColumnName;
}
}
// If the row does not have this element name then skip it
if (!row.Table.Columns.Contains(columnName))
{
continue;
}
// If the DataRow has a value with the same name, and it is not null, then assign it
object dbval = row[columnName];
if (dbval != null && !(dbval is DBNull))
{
pi.SetValue(rec, dbval, null);
}
}
}

Get the field list of flat file connection?

How to write a function (external function, c#, f# or powershell script, etc)
List<string> GetFields(string ssisPackageName, string fileSourceName);
to get the field list of a SSIS package? Since the package is an Xml file, can xquery be used to get the list?
Or even better, get more information,
class Field
{
public string Name { get; set; }
public string Type { get; set; }
}
List<Field> GetFields(string ssisPackageName, string fileSourceName);
#billinkc is right, you should keep data typing issues in mind. That said, you could at best retrieve the Code Page and Unicode values for the Flat File Connection Manager itself. The following code should get you started, where you might need some lookups for the code page and data type attributes.
string path = #"MyPathTo\Package.dtsx";
XNamespace dts = "www.microsoft.com/SqlServer/Dts";
XDocument doc = XDocument.Load(path);
// get all connections
var connections = from ele in doc.Descendants(dts + "ConnectionManager")
where ele.Attributes(dts + "ObjectName").Count() != 0
select ele;
foreach (var connection in connections)
{
// look for your flat file connection
if (connection.Attribute(dts + "ObjectName").Value == "Flat File Connection Manager")
{
var connectionDetails = connection.Element(dts + "ObjectData").Element(dts + "ConnectionManager");
Console.WriteLine("CodePage: " + connectionDetails.Attribute(dts + "CodePage").Value);
Console.WriteLine("Unicode: " + connectionDetails.Attribute(dts + "Unicode").Value);
var columnList = connection.Descendants(dts + "FlatFileColumn");
foreach (var column in columnList)
{
Console.WriteLine("Column name: " + column.Attribute(dts + "ObjectName").Value);
Console.WriteLine("Column type: " + column.Attribute(dts + "DataType").Value);
}
}
}

How to Parse BlobColumn into Words, while removing spaces and carriage returns, in SSIS? [duplicate]

Struggling with a C# Component. What I am trying to do is take a column that is ntext in my input source which is delimited with pipes, and then write the array to a text file. When I run my component my output looks like this:
DealerID,StockNumber,Option
161552,P1427,Microsoft.SqlServer.Dts.Pipeline.BlobColumn
Ive been working with the GetBlobData method and im struggling with it. Any help with be greatly appreciated! Here is the full script:
public override void Input0_ProcessInputRow(Input0Buffer Row)
{
string vehicleoptionsdelimited = Row.Options.ToString();
//string OptionBlob = Row.Options.GetBlobData(int ;
//string vehicleoptionsdelimited = System.Text.Encoding.GetEncoding(Row.Options.ColumnInfo.CodePage).GetChars(OptionBlob);
string[] option = vehicleoptionsdelimited.Split('|');
string path = #"C:\Users\User\Desktop\Local_DS_CSVs\";
string[] headerline =
{
"DealerID" + "," + "StockNumber" + "," + "Option"
};
System.IO.File.WriteAllLines(path + "OptionInput.txt", headerline);
using (System.IO.StreamWriter file = new System.IO.StreamWriter(path + "OptionInput.txt", true))
{
foreach (string s in option)
{
file.WriteLine(Row.DealerID.ToString() + "," + Row.StockNumber.ToString() + "," + s);
}
}
Try using
BlobToString(Row.Options)
using this function:
private string BlobToString(BlobColumn blob)
{
string result = "";
try
{
if (blob != null)
{
result = System.Text.Encoding.Unicode.GetString(blob.GetBlobData(0, Convert.ToInt32(blob.Length)));
}
}
catch (Exception ex)
{
result = ex.Message;
}
return result;
}
Adapted from:
http://mscrmtech.com/201001257/converting-microsoftsqlserverdtspipelineblobcolumn-to-string-in-ssis-using-c
Another very easy solution to this problem, because it is a total PITA, is to route the error output to a derived column component and cast your blob data to a to a STR or WSTR as a new column.
Route the output of that to your script component and the data will come in as an additional column on the pipeline ready for you to parse.
This will probably only work if your data is less than 8000 characters long.

SharpPcap - A Packet Capture getting messesge problem

I trying to capture packets using SharpPcap library.
I'm able to return the packets details but I'm having problem to get what the message content inside the packet.
the packet using .Data to return the message and when I use it it is returning (System.Byte[]).
here is the library website:
http://www.codeproject.com/KB/IP/sharppcap.aspx
here is my code:
string packetData;
private void packetCapturingThreadMethod()
{
Packet packet = null;
int countOfPacketCaptures = 0;
while ((packet = device.GetNextPacket()) != null)
{
packet = device.GetNextPacket();
if (packet is TCPPacket)
{
TCPPacket tcp = (TCPPacket)packet;
myPacket tempPacket = new myPacket();
tempPacket.packetType = "TCP";
tempPacket.sourceAddress = Convert.ToString(tcp.SourceAddress);
tempPacket.destinationAddress = Convert.ToString(tcp.DestinationAddress);
tempPacket.sourcePort = Convert.ToString(tcp.SourcePort);
tempPacket.destinationPort = Convert.ToString(tcp.DestinationPort);
tempPacket.packetMessage = Convert.ToString(tcp.Data);
packetsList.Add(tempPacket);
packetData =
"Type= TCP" +
" Source Address = "+ Convert.ToString(tcp.SourceAddress)+
" Destination Address =" +Convert.ToString(tcp.DestinationAddress)+
" SourcePort =" + Convert.ToString(tcp.SourcePort)+
" SourcePort =" +Convert.ToString(tcp.DestinationPort)+
" Messeage =" + Convert.ToString(tcp.Data);
txtpackets.Invoke(new UpdatetxtpacketsCallback(this.Updatetxtpackets),
new object[] { packetData });
string[] row = { packetsList[countOfPacketCaptures].packetType, packetsList[countOfPacketCaptures].sourceAddress, packetsList[countOfPacketCaptures].destinationAddress, packetsList[countOfPacketCaptures].sourcePort, packetsList[countOfPacketCaptures].destinationPort, packetsList[countOfPacketCaptures].packetMessage };
try { //dgwPacketInfo.Rows.Add(row); countOfPacketCaptures++;
//lblCapturesLabels.Text = Convert.ToString(countOfPacketCaptures);
}
catch (Exception e) { }
}
else if (packet is UDPPacket)
{
UDPPacket udp = (UDPPacket)packet;
myPacket tempPacket = new myPacket();
tempPacket.packetType = "UDP";
tempPacket.sourceAddress = Convert.ToString(udp.SourceAddress);
tempPacket.destinationAddress = Convert.ToString(udp.DestinationAddress);
tempPacket.sourcePort = Convert.ToString(udp.SourcePort);
tempPacket.destinationPort = Convert.ToString(udp.DestinationPort);
tempPacket.packetMessage = udp.Data.ToArray() + "\n";
packetsList.Add(tempPacket);
packetData =
"Type= UDP" +
" Source Address = "+ Convert.ToString(udp.SourceAddress)+
" Destination Address =" +Convert.ToString(udp.DestinationAddress)+
" SourcePort =" + Convert.ToString(udp.SourcePort)+
" SourcePort =" +Convert.ToString(udp.DestinationPort)+
" Messeage =" + udp.Data.ToArray() + "\n";
string[] row = { packetsList[countOfPacketCaptures].packetType, packetsList[countOfPacketCaptures].sourceAddress, packetsList[countOfPacketCaptures].destinationAddress, packetsList[countOfPacketCaptures].sourcePort, packetsList[countOfPacketCaptures].destinationPort, packetsList[countOfPacketCaptures].packetMessage };
try {
//dgwPacketInfo.Rows.Add(row);
//countOfPacketCaptures++;
//lblCapturesLabels.Text = Convert.ToString(countOfPacketCaptures);
txtpackets.Invoke(new UpdatetxtpacketsCallback(this.Updatetxtpackets),
new object[] { packetData });
}
catch (Exception e) { }
}
}
}
I found the answer...
Data is a byte array so I need to use bit converter and instead of using:
Convert.ToString(tcp.Data);
I should use:
BitConverter.ToString(tcp.Data)
The parser isn't that complex...
I looked at the Packet.Net code (which is the parse for SharpPcap) and all of the fields are stored in commonly used formats.
The IP Addresses are stored in System.Net.IPAddress format so you can just call .ToString on them to get a text string that properly includes the dot marks.
The port numbers are stored as ushort which can be printed the same as any other integer.
The only part that needs to be interpreted in its binary form is the Data field because that changes based on what protocol is being used on the next layer up. SharpPcap/Packet.Net does most of the work for you already and fields are stored in the most convenient or identical forms to those found in the protocol specification. Just use intellisense to check the field's type and if it's not one you're familiar with (such as System.Net.IPAddress or System.NetworkInformation.PhysicalAddress (For MAC addresses)) just google it.

Categories