使用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. 禁用窗体关闭按钮(使用GetWindowLong修改GWL_STYLE)

    一般我们不想让窗体能够关闭, 首先想到的是在OnCloseQuery事件里设置CanClose := False, 不过在某些情况下这个会和程序关闭窗体的业务逻辑产生冲突 所以写了下面这个函数, 可以 ...

  2. zz将 VSTO 插件部署给所有用户

    原文:zz将 VSTO 插件部署给所有用户 注:本文原作者 Misha Shneerson 是 VSTO 团队的工程师.原文可以在下列地址找到:http://blogs.msdn.com/mshnee ...

  3. Java 8 中的 Streams API 详解

    为什么需要 Stream Stream 作为 Java 8 的一大亮点,它与 java.io 包里的 InputStream 和 OutputStream 是完全不同的概念.它也不同于 StAX 对 ...

  4. Flesch Reading Ease(模拟)

    http://poj.org/problem?id=3371 终于遇到简单一点的模拟题了.不过本人真心没有耐心读题目... 它的大致意思就是给一段合法的文章,求出这段文章的单词数,句子数,音节数,按照 ...

  5. UVA- 1504 - Genghis Khan the Conqueror(最小生成树-好题)

    题意: n个点,m个边,然后给出m条边的顶点和权值,其次是q次替换,每次替换一条边,给出每次替换的边的顶点和权值,然后求出这次替换的最小生成树的值; 最后要你输出:q次替换的平均值.其中n<30 ...

  6. vim学习与理解

    1. 转变思维 --> vim 无鼠标文本编辑工具 在鸟哥的linux私房菜中,是这么说明linux的: 1. vim是linux like系统中非常强大的一个文本编辑工具,历史悠久,很多系统都 ...

  7. Search Insert Position——LeetCode

    Given a sorted array and a target value, return the index if the target is found. If not, return the ...

  8. android画虚线的自定义VIew

    package com.yesway.ycarplus.view; import android.annotation.SuppressLint; import android.content.Con ...

  9. [Locked] Flatten 2D Vector

    Problem Description: Implement an iterator to flatten a 2d vector. For example,Given 2d vector = [ [ ...

  10. Jenkins 四: 启动关闭以及重启jenkins

    启动 1. 在桌面新建一个jenkins.bat文件.内容如下: cd /d %JENKINS_HOME% java -jar %JENKINS_HOME%\jenkins.war --httpPor ...