使用WPF组件

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转换工具" Height="285" Width="276"
WindowStartupLocation="CenterScreen"
>
<Grid Margin="0,0,2,2">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="302*"/>
<ColumnDefinition Width="0*"/>
</Grid.ColumnDefinitions>
<Button Content="打开" HorizontalAlignment="Left" Margin="10,220,0,0" VerticalAlignment="Top" Width="75" Click="Open_Click" Height="22"/>
<Button Content="转换" HorizontalAlignment="Left" Margin="162,220,0,0" VerticalAlignment="Top" Width="75" Click="Convert_Click" Height="22" Name="butConvert" IsEnabled="False" />
<GroupBox Header="类别" 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 2007" 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="26" Width="200"/>
</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 2007(*.accdb)|*.accdb|所有文件(*.*)|*.*";
}
else if (Access2007.IsChecked.HasValue && (bool)Access2007.IsChecked == true)
{
op.Filter = "Access 2007(*.accdb)|*.accdb|Access 2003(*.mdb)|*.mdb|所有文件(*.*)|*.*";
}
else
{
op.Filter = "所有文件(*.*)|*.*|Access 2003(*.mdb)|*.mdb|Access 2007(*.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("转换完成.");
}
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>
/// 取所有表名
/// </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>
/// 取指定表所有字段名称
/// </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数据库数据转换Table.Json的更多相关文章

  1. django-将数据库数据转换成JSON格式(ORM和SQL两种情况)

    最近打算搞一个自动化运维平台,所以在看Django的知识. 在实际项目开发中,遇到一个小问题:前后端发生数据交互主流采用的是JSON.前端传数据到服务器端比较简单,稍微麻烦的是服务器端传JSON到前端 ...

  2. C#将Access数据库导出为JSON

    一个Access数据库包含若干首诗歌,每首诗有content.author.title.description四个字段 using System; using System.Data; using S ...

  3. PHP将数据库的数据转换成json格式

    header('content-type:application/json;charset=utf8');  $results = array();     while ($row = mysql_f ...

  4. 让ADO.NET Entity Framework 支持ACCESS数据库

    如写的不好请见谅,本人水平有限. 个人简历及水平:. http://www.cnblogs.com/hackdragon/p/3662599.html 接到一个程序和网页交互的项目,用ADO.NET ...

  5. 【.net 深呼吸】连接Access数据库应注意的几点

    本地数据库可以有Y种选择,比如Sqlite.SQL Server Express.SQL Local DB.SQL Server CE.Access等,本文老周选用比较著名的Access本地数据库,在 ...

  6. 创建ACCESS数据库,并且创建表和数据。重点:关闭ACCESS数据库引用

    /// <summary> /// 创建ACCESS数据库,并且创建表和数据 /// </summary> /// <param name="dictTable ...

  7. 对于Access数据库查询遇到空值的解决办法

    1.Access数据库在office环境下对于null是识别的,但是,在开发环境下,Access数据库对于where xxx is null是不识别的. 2.查询空值解决办法:select * fro ...

  8. 操作ACCESS数据库注意事项

    以下问题都是容易忽略,但却不容易找出问题的所在,让我头疼不少,故在此列出,即是一个总结,同样也给其他人参与! 1.使用参数形式执行SQL命令时,参数数组需与在SQL语句中参数名出现的位置及名称必须完全 ...

  9. 如何在Asp.net中备份Access数据库?

    public   void   Create(   string   mdbPath   ) { if(   File.Exists(mdbPath)   )   //检查数据库是否已存在 { thr ...

随机推荐

  1. CELL_PHOTO_IDENTIFIER

    # define CELL_PHOTO_IDENTIFIER @"photoLibraryCell" # define CLOSE_PHOTO_IMAGE @"close ...

  2. bzoj3531

    不难想到树链剖分这题的难点是记录的是路径上宗教相同的点裸的想法是对每一种宗教都开一棵线段树,记录每个点的评级但显然这样会爆空间,仔细分析一下,这些线段树内很多点压根就没用到因此我们考虑对线段树动态开点 ...

  3. bzoj2424

    比较简单的费用流,一目了然 ; type node=record next,point,flow,cost:longint; end; ..] of node; q:..] of longint; p ...

  4. 字符串(广义后缀自动机):BZOJ 3926 [Zjoi2015]诸神眷顾的幻想乡

    3926: [Zjoi2015]诸神眷顾的幻想乡 Time Limit: 10 Sec  Memory Limit: 512 MBSubmit: 843  Solved: 510[Submit][St ...

  5. HDU 5889 Barricade 【BFS+最小割 网络流】(2016 ACM/ICPC Asia Regional Qingdao Online)

    Barricade Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total S ...

  6. Maximum Subarray——LeetCode

    Find the contiguous subarray within an array (containing at least one number) which has the largest ...

  7. Python 实时日志平台 Sentry

    原文地址:http://www.oschina.net/p/sentry Sentry 是一个实时的事件日志和聚合平台,基于 Django 构建. Sentry 可以帮助你将 Python 程序的所有 ...

  8. bzoj 1602 [Usaco2008 Oct]牧场行走(LCA模板)

    1602: [Usaco2008 Oct]牧场行走 Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 379  Solved: 216[Submit][Sta ...

  9. centos 5 yum安装与配置vsFTPd FTP服务器

    vsftpd作为FTP服务器,在Linux系统中是非常常用的.下面我们介绍如何在centos系统上安装vsftp. 什么是vsftpd vsftpd是一款在Linux发行版中最受推崇的FTP服务器程序 ...

  10. CentOS虚拟机不能联网状况下yum方式从本地安装软件包(转载的)

    大家都知道yum是linux下一个非常好用的软件安装/卸载软件,它方便操作,而且最厉害的是可以解决令人头疼的包依赖关系.但是若是你的linux不能联网,若想使用yum安装软件,可以依照下面的方法. 1 ...