I know there a some other questions related to mine but still they do not help me very much. I tried to understand the concept of the String.Format Method but i failed over and over again and still have no clue how to use it in my case. I know it is asked very much but if someone has an idea how to get the alignment in place i would appreciate it a lot.
I'm saving the output of different database tables to different text files. I want to align the "name" of the column to its "value". This makes very much sense because in some tables there a lot of "null" values and that leads to a poorly formatted output. Because the "null" is not exported to that file and therefore the values are moved to the left.
private void WriteSQLQueryOutputToTextFile(string DBUser, string DBUserPassword, string sqlQuery, string databaseName, string nameOfOutputFile)
{
string pathOfOutputFile = dWTestResult + "\\DatabaseUpgradeCheck\\" + nameOfOutputFile;
using (SqlConnection sqlCon = new SqlConnection("Data Source=" +
GetEnvironmentVariable.MachineName + "; Initial Catalog=" + databaseName + "; User ID=" + DBUser + "; Password=" + DBUserPassword + ";"))
{
SqlDataAdapter adapter = new SqlDataAdapter(sqlQuery, sqlCon);
try
{
sqlCon.Open();
DataSet dataset = new DataSet();
adapter.Fill(dataset, "nameOfDataset");
string currentLine = "";
foreach (var col in dataset.Tables[0].Columns)
{
currentLine += " " + col.ToString();
}
OutputHandler.AppendDataToFile(pathOfOutputFile, currentLine);
foreach (DataRow row in dataset.Tables[0].Rows)
{
currentLine = "";
foreach (var item in row.ItemArray)
{
currentLine += " " + item.ToString();
}
OutputHandler.AppendDataToFile(pathOfOutputFile, currentLine);
}
}
catch (Exception ex)
{
logger.Debug(ex, "Writing Database Output to the " + "\"" + nameOfOutputFile + "\"" + " file failed");
}
finally
{
sqlCon.Close();
}
}
}
string.Format is good when you know your required format in advance, less good if you have dynamic sources like the results of a query. Try something like this:
private void WriteSQLQueryOutputToTextFile(string DBUser, string DBUserPassword, string sqlQuery, string databaseName, string nameOfOutputFile)
{
string pathOfOutputFile = dWTestResult + "\\DatabaseUpgradeCheck\\" + nameOfOutputFile;
using (SqlConnection sqlCon = new SqlConnection("Data Source=" +
GetEnvironmentVariable.MachineName + "; Initial Catalog=" + databaseName + "; User ID=" + DBUser + "; Password=" + DBUserPassword + ";"))
{
SqlDataAdapter adapter = new SqlDataAdapter(sqlQuery, sqlCon);
try
{
sqlCon.Open();
DataSet dataset = new DataSet();
adapter.Fill(dataset, "nameOfDataset");
string currentLine = "";
var nameLengths = new int[dataset.Tables[0].Columns.Count];
var i = 0;
foreach (var col in dataset.Tables[0].Columns)
{
var colName = col.ToString();
nameLengths[i] = colName.Length;
currentLine += " " + colName;
i++;
}
OutputHandler.AppendDataToFile(pathOfOutputFile, currentLine);
foreach (DataRow row in dataset.Tables[0].Rows)
{
currentLine = "";
i = 0;
foreach (var item in row.ItemArray)
{
var field = item.ToString();
currentLine += " " + field.PadRight(nameLengths[i], ' ');
i++;
}
OutputHandler.AppendDataToFile(pathOfOutputFile, currentLine);
}
}
catch (Exception ex)
{
logger.Debug(ex, "Writing Database Output to the " + "\"" + nameOfOutputFile + "\"" + " file failed");
}
finally
{
sqlCon.Close();
}
}
}
If you get the length of the string representation of every column name and data value, you can work out the minimum width for each column and prepare a format string for that column which sets its minimum width.
Something like this:
using System;
using System.Data;
using System.Linq;
using System.Text;
namespace ConsoleApp1
{
class Program
{
static void Main(string[] args)
{
DataTable dt = SampleData();
// Prepare the column formats
int nCols = dt.Columns.Count;
var dataWidths = dt.Columns.Cast<DataColumn>().Select(x => x.ColumnName.Length).ToList();
foreach (DataRow d in dt.Rows)
{
for (int i = 0; i < nCols; i++)
{
dataWidths[i] = Math.Max(dataWidths[i], d.ItemArray[i].ToString().Length);
}
}
var colFormats = dataWidths.Select(x => $"{{0,{-x}}}").ToList();
//
var sb = new StringBuilder();
// Add the column names
sb.AppendLine(string.Join(" ", dt.Columns.Cast<DataColumn>().Select((x, i) => string.Format(colFormats[i], x.ColumnName))));
// Add in the data
foreach (DataRow d in dt.Rows)
{
sb.AppendLine(string.Join(" ", d.ItemArray.Select((x, i) => string.Format(colFormats[i], x))));
}
Console.WriteLine(sb.ToString());
Console.ReadLine();
}
static DataTable SampleData()
{
DataTable sdt = new DataTable();
string[] cn = "ARCHIVE DBDATETIME NEXTDOCID HIGHESTDISK SYSTEMTYPE FLAGS VERSION SINGLEUSER".Split(' ');
foreach (string n in cn)
{
sdt.Columns.Add(n);
}
DataRow drn = sdt.NewRow();
drn["ARCHIVE"] = "Hello";
drn["DBDATETIME"] = 1316859;
drn["NEXTDOCID"] = 1;
drn["HIGHESTDISK"] = "Nothing";
drn["SYSTEMTYPE"] = 1;
drn["FLAGS"] = "ABC";
drn["VERSION"] = "Hello";
drn["SINGLEUSER"] = 256;
sdt.Rows.Add(drn);
drn = sdt.NewRow();
drn["ARCHIVE"] = "Value longer than header";
// No value for drn["DBDATETIME"] etc.
drn["SINGLEUSER"] = 256;
sdt.Rows.Add(drn);
return sdt;
}
}
}
Sample output:
ARCHIVE DBDATETIME NEXTDOCID HIGHESTDISK SYSTEMTYPE FLAGS VERSION SINGLEUSER
Hello 1316859 1 Nothing 1 ABC Hello 256
Value longer than header 256
Related
i have the following c# script on ssis project in visual studio that generates files based on a column value of a sql table and i would like to add a part that also splits the files when the record count reaches 200..Example of an extraction would be nameoffile_columnvalue_datetime_1.txt for the first 200,nameoffile_columnvalue_datetime_2.txt for the next 200 etc... thank you in advance!
public void Main()
{
// TODO: Add your code here
string datetime_1 = DateTime.Now.ToString("dd/MM/yyyy");
string datetime = datetime_1.Replace("/", String.Empty);
try
{
//Declare Variables
string FileNamePart = Dts.Variables["User::FlatFileNamePart"].Value.ToString();
string DestinationFolder = Dts.Variables["User::DestinationFolder"].Value.ToString();
string TableName = Dts.Variables["User::TableName"].Value.ToString();
string FileDelimiter = Dts.Variables["User::FileDelimiter"].Value.ToString();
string FileExtension = Dts.Variables["User::FileExtension"].Value.ToString();
string ColumnNameForGrouping = Dts.Variables["User::ColumnNameForGrouping"].Value.ToString();
string SubFolder = Dts.Variables["User::SubFolder"].Value.ToString();
Int32 RecordCntPerFile = (Int32)Dts.Variables["User::RecordsPerFile"].Value;
string RecordCntPerFileDecimal = RecordCntPerFile + ".0";
//USE ADO.NET Connection from SSIS Package to get data from table
SqlConnection myADONETConnection = new SqlConnection();
myADONETConnection = (SqlConnection)(Dts.Connections["AR_GSLO_OLTP"].AcquireConnection(Dts.Transaction) as SqlConnection);
//Read distinct Group Values for each flat file
string query = "Select distinct " + ColumnNameForGrouping + " from " + TableName;
//MessageBox.Show(query.ToString());
SqlCommand cmd = new SqlCommand(query, myADONETConnection);
//myADONETConnection.Open();
DataTable dt = new DataTable();
dt.Load(cmd.ExecuteReader());
myADONETConnection.Close();
//Loop through values for ColumnNameForGroup
foreach (DataRow dt_row in dt.Rows)
{
string ColumnValue = "";
object[] array = dt_row.ItemArray;
ColumnValue = array[0].ToString();
//Load Data into DataTable from SQL ServerTable
string queryString =
"SELECT * from " + TableName + " Where " + ColumnNameForGrouping + "='" + ColumnValue + "'";
SqlDataAdapter adapter = new SqlDataAdapter(queryString, myADONETConnection);
DataSet ds = new DataSet();
adapter.Fill(ds);
foreach (DataTable d_table in ds.Tables)
{
string FileFullPath = SubFolder + "\\" + FileNamePart + "_" + ColumnValue + "_" + datetime + FileExtension;
StreamWriter sw = null;
sw = new StreamWriter(FileFullPath, false);
// Write the Header Row to File an thelw na exei kai header ta kanw uncomment apo 152 mexri 160 grammi
int ColumnCount = d_table.Columns.Count;
// for (int ic = 0; ic < ColumnCount; ic++)
//{
// sw.Write(d_table.Columns[ic]);
// if (ic < ColumnCount - 1)
// {
// sw.Write(FileDelimiter);
// }
//}
// sw.Write(sw.NewLine);
// Write All Rows to the File
foreach (DataRow dr in d_table.Rows)
{
for (int ir = 0; ir < ColumnCount; ir++)
{
if (!Convert.IsDBNull(dr[ir]))
{
sw.Write(dr[ir].ToString());
}
if (ir < ColumnCount - 1)
{
sw.Write(FileDelimiter);
}
}
sw.Write(sw.NewLine);
}
sw.Close();
}
}
}
catch (Exception exception)
{
// Create Log File for Errors
using (StreamWriter sw = File.CreateText(Dts.Variables["User::LogFolder"].Value.ToString() + "\\" +
"ErrorLog_" + datetime + ".log"))
{
sw.WriteLine(exception.ToString());
Dts.TaskResult = (int)ScriptResults.Failure;
}
}
}
So assuming/understanding you have a script task which is the source of your rows?
You can add a Rowcount Task to capture the amount of rows in a variable for later validation.
after that, you might need a new script task where you can do the validation of how many records there is. something like a case statement/if statement or a type of loop for each 200.. odd records to change a value/filename.
Using that loop, you can change the filename/filenamevariable.
So you would need your ConnectionManager File Destination to have an expression with this variable filename.
Thus as soon as it hits 200.. records, the script should change the filename and the output should create a new file.
I am not sure if it will like the in flight name change. but think this might be a way to point your thoughts.
i did it first i used a foreach loop container to take the name of the txts as input variables
then inside the loop i created a script with the following c# code
public void Main()
{
// TODO: Add your code here
string datetime_1 = DateTime.Now.ToString("dd/MM/yyyy");
string datetime = datetime_1.Replace("/", String.Empty);
StreamWriter writer = null;
try
{
//Declare Variables
string filename= Dts.Variables["User::FileName"].Value.ToString();
//MessageBox.Show(filename);
using (StreamReader inputfile = new System.IO.StreamReader(filename))
{
int count = 0;
int filecount = 0;
string line;
while ((line = inputfile.ReadLine()) != null)
{
if (writer == null || count > 199)
{
count = 0;
if (writer != null)
{
writer.Close();
writer = null;
}
++filecount;
writer = new System.IO.StreamWriter(filename.Replace(".txt", "_") + filecount.ToString() + ".txt", true);
}
writer.WriteLine(line);
++count;
}
}
}
catch (Exception exception)
{
// Create Log File for Errors
using (StreamWriter sw = File.CreateText(Dts.Variables["User::LogFolder"].Value.ToString() + "\\" +
"ErrorLog_" + datetime + ".log"))
{
sw.WriteLine(exception.ToString());
Dts.TaskResult = (int)ScriptResults.Failure;
}
}
finally
{
if (writer != null)
writer.Close();
}
Dts.TaskResult = (int)ScriptResults.Success;
}
and that was it!
I have a csv file that has many string file to this format : for example "1,125"
when I read Csv These strings are empty.
string header = isFirstRowHeader ? "Yes" : "No";
string pathOnly = Path.GetDirectoryName(path);
string fileName = Path.GetFileName(path);
string sql = #"SELECT * FROM [" + fileName + "]";
using (OleDbConnection connection = new OleDbConnection(
#"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + pathOnly +
";Extended Properties=\"Text;HDR=" + header + "\""))
using (OleDbCommand command = new OleDbCommand(sql, connection))
using (OleDbDataAdapter adapter = new OleDbDataAdapter(command))
{
DataTable dataTable = new DataTable();
dataTable.Locale = CultureInfo.CurrentCulture;
adapter.Fill(dataTable);
}
my CSV file is like this:
ABS,15,1392/12/12,501,"1,205",KTI1212,"123,5",KK
Check this code:
try
{
//Read the file
System.IO.StreamReader f = new System.IO.StreamReader(fileName, System.Text.Encoding.UTF8);
string line = f.ReadLine();
//Split text by comma
string[] tokens = line.Split(',');
int tokenCount = tokens.Length;
//Reconstruct the broken strings - tokensList retreives the final values
System.Collections.ArrayList tokensList = new System.Collections.ArrayList();
string token = "";
bool stringFound = false;
string currentToken;
for (int i=0; i<tokenCount; i++)
{
currentToken = tokens[i];
if (!stringFound)
{
//Check if not a string
if ((currentToken.Trim().StartsWith("\"") && !currentToken.Trim().EndsWith("\"")) || currentToken.Trim().Equals("\""))
{
stringFound = true;
token = currentToken;
}
else
{
//Add the string as final value
tokensList.Add(currentToken);
}
}
else
{
//Reconstruct the string
token += "," + currentToken;
if (currentToken.Trim().EndsWith("\""))
{
int quoteIndex = currentToken.LastIndexOf("\"");
if (quoteIndex == 0 || (quoteIndex > 0 && currentToken[quoteIndex-1] != '"'))
{
tokensList.Add(token);
stringFound = false;
token = "";
}
}
}
}
}
catch (Exception ex)
{
Console.WriteLine(ex.StackTrace);
}
I am looking for a way to create, modify, read .xlsx files in C# without installing Excel or creating files on the server before giving to the user to download.
I found NPOI http://npoi.codeplex.com/ which looks great but supports .xls not .xlsx
I found ExcelPackage http://excelpackage.codeplex.com/ which looks great but has the additional overhead of creating the file on the server before it can be sent to the user.
Does anyone know of a way around this?
I found EPPlus http://epplus.codeplex.com but I am not not certain if this requires creation of a file on the server before it can be sent to the user?
I am pretty new to this so any guidance/examples etc., would be very much appreciated.
With EPPlus it's not required to create file, you can do all with streams, here is an example of ASP.NET ashx handler that will export datatable into excel file and serve it back to the client :
public class GetExcel : IHttpHandler
{
public void ProcessRequest(HttpContext context)
{
var dt = DBServer.GetDataTable("select * from table");
var ms = GetExcel.DataTableToExcelXlsx(dt, "Sheet1");
ms.WriteTo(context.Response.OutputStream);
context.Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
context.Response.AddHeader("Content-Disposition", "attachment;filename=EasyEditCmsGridData.xlsx");
context.Response.StatusCode = 200;
context.Response.End();
}
public bool IsReusable
{
get
{
return false;
}
}
public static MemoryStream DataTableToExcelXlsx(DataTable table, string sheetName)
{
var result = new MemoryStream();
var pack = new ExcelPackage();
var ws = pack.Workbook.Worksheets.Add(sheetName);
int col = 1;
int row = 1;
foreach (DataRow rw in table.Rows)
{
foreach (DataColumn cl in table.Columns)
{
if (rw[cl.ColumnName] != DBNull.Value)
ws.Cells[row, col].Value = rw[cl.ColumnName].ToString();
col++;
}
row++;
col = 1;
}
pack.SaveAs(result);
return result;
}
}
http://msdn.microsoft.com/en-us/library/cc850837.aspx
Try to use this code to export the data to excel, may it ll help
public static void DataSetsToExcel(DataSet dataSet, string filepath)
{
try
{
string connString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + filepath + ";Extended Properties=Excel 12.0 Xml;";
string tablename = "";
DataTable dt = new DataTable();
foreach (System.Data.DataTable dataTable in dataSet.Tables)
{
dt = dataTable;
tablename = dataTable.TableName;
using (OleDbConnection con = new OleDbConnection(connString))
{
con.Open();
StringBuilder strSQL = new StringBuilder();
strSQL.Append("CREATE TABLE ").Append("[" + tablename + "]");
strSQL.Append("(");
for (int i = 0; i < dt.Columns.Count; i++)
{
strSQL.Append("[" + dt.Columns[i].ColumnName + "] text,");
}
strSQL = strSQL.Remove(strSQL.Length - 1, 1);
strSQL.Append(")");
OleDbCommand cmd = new OleDbCommand(strSQL.ToString(), con);
cmd.ExecuteNonQuery();
for (int i = 0; i < dt.Rows.Count; i++)
{
strSQL.Clear();
StringBuilder strfield = new StringBuilder();
StringBuilder strvalue = new StringBuilder();
for (int j = 0; j < dt.Columns.Count; j++)
{
strfield.Append("[" + dt.Columns[j].ColumnName + "]");
strvalue.Append("'" + dt.Rows[i][j].ToString().Replace("'", "''") + "'");
if (j != dt.Columns.Count - 1)
{
strfield.Append(",");
strvalue.Append(",");
}
else
{
}
}
if (strvalue.ToString().Contains("<br/>"))
{
strvalue = strvalue.Replace("<br/>", Environment.NewLine);
}
cmd.CommandText = strSQL.Append(" insert into [" + tablename + "]( ")
.Append(strfield.ToString())
.Append(") values (").Append(strvalue).Append(")").ToString();
cmd.ExecuteNonQuery();
}
con.Close();
}
}
}
catch (Exception ex)
{
}
}
public DataTable ExcelToDatatable_dt1
{
get
{
foreach (GridViewRow rowItem in GridView1.Rows)
{
CheckBox chk;
// gets the Debit Reference number for each row checked
string productId = GridView1.DataKeys[rowItem.RowIndex].Value.ToString();
chk = (CheckBox)(rowItem.Cells[0].FindControl("RowChecker"));
if (chk.Checked)
{
string fileName = Session["fileName"].ToString();
//string fileName = "Collection.csv";
// put the file name here
string strSql = "SELECT * FROM [" + fileName + "] WHERE DDIREF = ["+ productId +]";
string strCSVConnString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + Server.MapPath(".\\Files\\") + ";" + "Extended Properties='text;HDR=YES;'";
using (FileStream filestr = new FileStream(Server.MapPath(".\\Files\\") + "\\schema.ini",
FileMode.Create, FileAccess.Write))
{
using (StreamWriter writer = new StreamWriter(filestr))
{
writer.WriteLine("[" + fileName + "]");
writer.WriteLine("ColNameHeader=False");
writer.WriteLine("Format=CSVDelimited");
writer.WriteLine("DateTimeFormat=dd-MMM-yy");
//Message_ltr.Text += positionList[0].ToString();
for (int i = 0; i < 29; i++)
//if (((positionList[i].ToString() != null) && (positionList[i].ToString() != "")) && ((nameList[i].ToString() != null) && (nameList[i].ToString() != "")))
writer.WriteLine("Col" + positionList[i].ToString() + "=" + nameList[i].ToString() + " Text Width 100");
//writer.WriteLine("Col2=SortCode Text Width 30");
writer.Close();
writer.Dispose();
}
filestr.Close();
filestr.Dispose();
}
OleDbDataAdapter oleda = new OleDbDataAdapter(strSql, strCSVConnString);
DataTable dtbCSV = new DataTable();
oleda.Fill(dtbCSV);
return dtbCSV;
}
}
return null;// instead of null I want to return dtbCSV here
}
This gives an error
"Error 45 'DirectDebit.Interbacs.CompanyUpload.ExcelToDatatable_dt1.get': not all code paths return a value".
The error is due to the return statement which is not in the correct scope. But the problem is, I want to return dtbCSV and I am not able return that within the "get" scope as dtCSV does not exist within the get context, I have no knowledge of passing the values between scopes within a return method.
Try putting DataTable dtbCSV = new DataTable(); before your foreach statement (and remove the other declaration).
Edit
Note: This will put dtbCSV in the scope of the entire get block instead of just in your if statement.
We have a third party application that accept calls using an XML RPC mechanism for calling stored procs.
We send a ZIP-compressed dataset containing multiple tables with a bunch of update/delete/insert using this mechanism. On the other end, a CLR sproc decompress the data and gets the dataset.
Then, the following code gets executed:
using (var conn = new SqlConnection("context connection=true"))
{
if (conn.State == ConnectionState.Closed)
conn.Open();
try
{
foreach (DataTable table in ds.Tables)
{
string columnList = "";
for (int i = 0; i < table.Columns.Count; i++)
{
if (i == 0)
columnList = table.Columns[0].ColumnName;
else
columnList += "," + table.Columns[i].ColumnName;
}
var da = new SqlDataAdapter("SELECT " + columnList + " FROM " + table.TableName, conn);
var builder = new SqlCommandBuilder(da);
builder.ConflictOption = ConflictOption.OverwriteChanges;
da.RowUpdating += onUpdatingRow;
da.Update(ds, table.TableName);
}
}
catch (....)
{
.....
}
}
Here's the event handler for the RowUpdating event:
public static void onUpdatingRow(object sender, SqlRowUpdatingEventArgs e)
{
if ((e.StatementType == StatementType.Update) && (e.Command == null))
{
e.Command = CreateUpdateCommand(e.Row, sender as SqlDataAdapter);
e.Status = UpdateStatus.Continue;
}
}
and the CreateUpdateCommand method:
private static SqlCommand CreateUpdateCommand(DataRow row, SqlDataAdapter da)
{
string whereClause = "";
string setClause = "";
SqlConnection conn = da.SelectCommand.Connection;
for (int i = 0; i < row.Table.Columns.Count; i++)
{
char quoted;
if ((row.Table.Columns[i].DataType == Type.GetType("System.String")) ||
(row.Table.Columns[i].DataType == Type.GetType("System.DateTime")))
quoted = '\'';
else
quoted = ' ';
string val = row[i].ToString();
if (row.Table.Columns[i].DataType == Type.GetType("System.Boolean"))
val = (bool)row[i] ? "1" : "0";
bool isPrimaryKey = false;
for (int j = 0; j < row.Table.PrimaryKey.Length; j++)
{
if (row.Table.PrimaryKey[j].ColumnName == row.Table.Columns[i].ColumnName)
{
if (whereClause != "")
whereClause += " AND ";
if (row[i] == DBNull.Value)
whereClause += row.Table.Columns[i].ColumnName + "=NULL";
else
whereClause += row.Table.Columns[i].ColumnName + "=" + quoted +
val + quoted;
isPrimaryKey = true;
break;
}
}
/* Only values for column that is not a primary key can be modified */
if (!isPrimaryKey)
{
if (setClause != "")
setClause += ", ";
if (row[i] == DBNull.Value)
setClause += row.Table.Columns[i].ColumnName + "=NULL";
else
setClause += row.Table.Columns[i].ColumnName + "=" + quoted +
val + quoted;
}
}
return new SqlCommand("UPDATE " + row.Table.TableName + " SET " + setClause + " WHERE " + whereClause, conn);
}
However, this is really slow when we have a lot of records.
Is there a way to optimize this or an entirely different way to send lots of udpate/delete on several tables? I would really much like to use TSQL for this but can't figure a way to send a dataset to a regular sproc.
Additional notes:
We cannot directly access the SQLServer database.
We tried without compression and it was slower.
If you're using SQL Server 2008, you should look into MERGE
Look here for more info on MERGE