Working with WPF components
Xaml
<window x:class= "Jsonconvert.mainwindow"
Xmlns= "Http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x= "Http://schemas.microsoft.com/winfx/2006/xaml"
Title= "JSON conversion Tool" height= "285" width= "276"
windowstartuplocation= "Centerscreen"
>
<grid margin= "0,0,2,2" >
<Grid.ColumnDefinitions>
<columndefinition width= "302*"/>
<columndefinition width= "0*"/>
</Grid.ColumnDefinitions>
<button content= "Open" horizontalalignment= "left" margin= "10,220,0,0" verticalalignment= "Top" width= "Click=" Open_click "height="/>
<button content= "Convert" horizontalalignment= "left" margin= "162,220,0,0" verticalalignment= "Top" width= "Click=" Convert_click "height=" "Name=" Butconvert "isenabled=" False "/>
<groupbox header= "category" Horizontalalignment= "left" margin= "10,10,0,0" verticalalignment= "Top" height= "152.164" Width= "227" >
<grid margin= "0,0,-2,-4" >
<Grid.ColumnDefinitions>
<columndefinition width= "27*"/>
<columndefinition width= "127*"/>
<columndefinition width= "63*"/>
</Grid.ColumnDefinitions>
<radiobutton groupname= "FileType" name= "Access2003" content= "Access 2003" horizontalalignment= "left" margin= " 10,10,0,0 "verticalalignment=" Top "ischecked=" True "grid.columnspan=" 2 "/>
<radiobutton groupname= "FileType" name= "Access2007" content= "Access" horizontalalignment= "left" margin= " 10,40.836,0,0 "verticalalignment=" Top "grid.columnspan=" 2 "/>
</Grid>
</GroupBox>
<label content= "" Name= "Labpath" horizontalalignment= "left" margin= "10,180,0,0" verticalalignment= "Top" Height= " "Width=" "/>"
</Grid>
</Window>
Cs
private string FileName;
Public MainWindow ()
{
InitializeComponent ();
}
private void Open_click (object sender, RoutedEventArgs e)
{
Microsoft.Win32.OpenFileDialog op = new Microsoft.Win32.OpenFileDialog ();
Op. InitialDirectory = @ "C: \";
Op. Restoredirectory = true;
if (Access2003.IsChecked.HasValue && (bool) access2003.ischecked = = True)
{
Op. Filter = "Access 2003 (*.mdb) |*.mdb| Access *.accdb |*.accdb| All Files (*. *) |*.* ";
}
else if (Access2007.IsChecked.HasValue && (bool) access2007.ischecked = = True)
{
Op. Filter = "Access (*.accdb) |*.accdb| Access 2003 (*.mdb) |*.mdb| All Files (*. *) |*.* ";
}
Else
{
Op. Filter = "All Files (*. *) |*.*| Access 2003 (*.mdb) |*.mdb| Access (*.accdb) |*.accdb ";
}
Op. ShowDialog ();
FileName = Op. FileName;
if (!string. IsNullOrEmpty (fileName) | | File.exists (FileName))
{
Butconvert.isenabled = true;
Labpath.content = FileName;
}
}
private void Convert_click (object sender, RoutedEventArgs e)
{
String strconnection = String. Empty;
if (Access2003.IsChecked.HasValue && (bool) access2003.ischecked = = True)
{
ConnStr = strConnection03 + @ "Data Source =" + fileName;
}
if (Access2007.IsChecked.HasValue && (bool) access2007.ischecked = = True)
{
ConnStr = StrConnection07 + @ "Data Source =" + fileName;
}
if (string. IsNullOrEmpty (CONNSTR))
{
Return
}
Try
{
String path = filename.substring (0, filename.lastindexof (' \ \ '));
foreach (Var tableName in Gettablenamelist ())
{
var dt = GetTable (tableName);
var json = Getjson (DT);
var file = path + "\ \" + TableName + ". JSON";
File.writealltext (file, JSON);
}
MessageBox.Show ("conversion complete.");
}
catch (Exception ex)
{
MessageBox.Show (ex. Message);
}
}
private string ConnStr;
Private Const string StrConnection07 = "Provider = microsoft.ace.oledb.12.0;";
Private Const string STRCONNECTION03 = "Provider = microsoft.jet.oledb.4.0;";
<summary>
Take all table names
</summary>
<returns></returns>
Public list<string> gettablenamelist ()
{
list<string> list = new list<string> ();
OleDbConnection Conn = new OleDbConnection (CONNSTR);
Try
{
if (conn.state = = connectionstate.closed)
Conn.Open ();
DataTable dt = Conn.getschema ("Tables");
foreach (DataRow row in dt. Rows)
{
if (Row[3]. ToString () = = "TABLE")
List. ADD (Row[2]. ToString ());
}
return list;
}
catch (Exception e)
{throw e;}
finally {if (conn.state = = ConnectionState.Open) conn.close (); Conn.dispose (); }
}
//<summary>
//Fetch all field names for the specified table
//</summary>
//<returns></returns>
Public DataTable GetTable (String TableName)
{
OleDbConnection Conn = new OleDbConnection (connstr);
Try
{
if ( Conn.state = = connectionstate.closed)
Conn.Open ();
using (OleDbCommand cmd = new OleDbCommand ())
{
cmd. CommandText = "select * FROM [" + TableName + "]";
Cmd. Connection = Conn;
OleDbDataAdapter da = new OleDbDataAdapter (cmd);
DataSet ds = new DataSet ();
da. Fill (DS);
if (ds. Tables.count > 0)
return DS. Tables[0];
Else
return null;
}
}
catch (Exception e)
{throw e;}
Finally
{
if (conn.state = = ConnectionState.Open)
Conn.close ();
Conn.dispose ();
}
}
public string Getjson (DataTable dt)
{
System.Web.Script.Serialization.JavaScriptSerializer serializer = new System.Web.Script.Serialization.JavaScriptSerializer ();
Serializer. maxjsonlength = Int. MaxValue;
list<dictionary<string, object>> rows = new list<dictionary<string, object>> ();
dictionary<string, object> row = null;
foreach (DataRow dr in Dt. Rows)
{
row = new dictionary<string, object> ();
foreach (DataColumn col in dt. Columns)
{
Row. Add (Col. Columnname.trim (), Dr[col]);
}
Rows. ADD (row);
}
Return serializer. Serialize (rows);
}
Access database Data Conversion Table.json