Saturday, February 7, 2009

ADO Connection String for Access 2007 Database

ADO Connection String for Access 2007 Database

Here is the ADO connection string example for Access 2007 database.

Set Cn = New ADODB.Connection

Cn.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\comp\Documents\SampleDB.accdb;Persist Security Info=False"

If the database needs password then use the following string:

Cn.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\comp\Documents\SampleDB.accdb; Jet OLEDB:Database Password=MyDbPassword;"

The project that uses the code should have reference to ActiveX data objects library

Monday, February 2, 2009

ADO Connection String for Excel

Excel Connection Strings

"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=D:\Orginal.xls;Extended Properties=Excel 8.0;Persist Security Info=False"

ADO Connection String for Text Files

ADO Connection String for Comma Separate File (CSV)

"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=c:\temp\;Extended Properties=""text;HDR=Yes;FMT=Delimited(,)"";Persist Security Info=False"


Thursday, January 29, 2009

How to Convert CSV to XML using C#

C# CSV to XML Conversion, CSV to XML Conversion using LINQ


Language Integrated Query (LINQ) can be used to convert a CSV file to XML. Here is the sample CSV file that needs to be converted

The following code reads the CSV file to an array. LINQ is used to loop through the array and the contents are written as XML using XElement (System.XML.Linq).


public void ConvertCSVToXML()

{

String[] FileContent = File.ReadAllLines(@"C:\Temp\vba.csv");

String XMLNS = "";

XElement Inv = new XElement("Invoice",

from items in FileContent

let fields = items.Split(',')

select new XElement("Item",

new XElement("ID", fields[0]),

new XElement("Name", fields[1]),

new XElement("Price", fields[2]),

new XElement("Availability", fields[3]),

new XElement("TotalPrice", fields[4])

)

);

File.WriteAllText(@"C:\Temp\vba.xml", XMLNS + Inv.ToString() );

}

Here is the Input Text File


The output XML looks like


LINQ and C#, C# Convert CSV To XML, .NET CSV To XML, LINQ CSV to XML, Convert Text files to XML using C#,

List files based on FileSize using C#

C# Get Files Greater than Specific Size using Linq


C# and LINQ can be combined to do wonders; here is a sample of that. The following snippet extracts the files that are more thsn 345 KB

private static void GetFilesFromDirectory(string DirPath)

{

try

{

DirectoryInfo Dir = new DirectoryInfo(DirPath);

FileInfo[] FileList = Dir.GetFiles("*.*", SearchOption.TopDirectoryOnly );

var query = from FI in FileList

where FI.Length > 340000

select FI.FullName + " " + FI.Length.ToString() ;

foreach (string s1 in query )

{

Console.WriteLine(s1);

}

}

catch (Exception ex)

{

Console.WriteLine(ex.Message );

}

}

Extract files based on Size in C#, Extract files greater than 1GB using C#, C# List files based on Size, Filter by FileSize using C#, Extract files based on Size in .NET, Extract files greater than 1GB using .NET, .NET List files based on Size,

C# GetFiles with Date Filter

How to Filter files that are modified in a specific day using C#

Here is a snippet using C# and LINQ, which will retrieve the files from a directory for a specific date

private static void GetFilesFromDirectory(string DirPath)

{

try

{

DirectoryInfo Dir = new DirectoryInfo(DirPath);

FileInfo[] FileList = Dir.GetFiles("*.*", SearchOption.TopDirectoryOnly );

var query = from FI in FileList

where FI.LastWriteTime.Date == DateTime.Now.Date

select FI.FullName + " " + FI.LastWriteTime;

foreach (string s1 in query )

{

Console.WriteLine(s1);

}

}

catch (Exception ex)

{

Console.WriteLine(ex.Message );

}

}



C# Get Read Only Files

List all ReadOnly files in a directory using C# (.NET)

The following code will retrieve the read-only files using LINQ and C#

private static void GetFilesFromDirectory(string DirPath)

{

try

{

DirectoryInfo Dir = new DirectoryInfo(DirPath);

FileInfo[] FileList = Dir.GetFiles("*.*", SearchOption.TopDirectoryOnly );

var query = from FI in FileList

where FI.IsReadOnly == true

select FI.FullName + " " + FI.LastWriteTime;

foreach (string s1 in query )

{

Console.WriteLine(s1);

}

}

catch (Exception ex)

{

Console.WriteLine(ex.Message );

}

}

The above can (and mostly is) done by the following way

FileInfo[] FileList = Dir.GetFiles("*.*", SearchOption.TopDirectoryOnly );

foreach (FileInfo F1 in FileList)

{

if (F1.IsReadOnly == true)

{

Console.WriteLine(F1.FullName );

}

}

List ReadOnly files using C#, Retrieve Read-Only files using C#, Get Read Only Files using C#, List ReadOnly files using .NET, Retrieve Read-Only files using .NET, Get Read Only Files using .NET