Access数据库数据转换Table.Json
使用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的更多相关文章
- django-将数据库数据转换成JSON格式(ORM和SQL两种情况)
最近打算搞一个自动化运维平台,所以在看Django的知识. 在实际项目开发中,遇到一个小问题:前后端发生数据交互主流采用的是JSON.前端传数据到服务器端比较简单,稍微麻烦的是服务器端传JSON到前端 ...
- C#将Access数据库导出为JSON
一个Access数据库包含若干首诗歌,每首诗有content.author.title.description四个字段 using System; using System.Data; using S ...
- PHP将数据库的数据转换成json格式
header('content-type:application/json;charset=utf8'); $results = array(); while ($row = mysql_f ...
- 让ADO.NET Entity Framework 支持ACCESS数据库
如写的不好请见谅,本人水平有限. 个人简历及水平:. http://www.cnblogs.com/hackdragon/p/3662599.html 接到一个程序和网页交互的项目,用ADO.NET ...
- 【.net 深呼吸】连接Access数据库应注意的几点
本地数据库可以有Y种选择,比如Sqlite.SQL Server Express.SQL Local DB.SQL Server CE.Access等,本文老周选用比较著名的Access本地数据库,在 ...
- 创建ACCESS数据库,并且创建表和数据。重点:关闭ACCESS数据库引用
/// <summary> /// 创建ACCESS数据库,并且创建表和数据 /// </summary> /// <param name="dictTable ...
- 对于Access数据库查询遇到空值的解决办法
1.Access数据库在office环境下对于null是识别的,但是,在开发环境下,Access数据库对于where xxx is null是不识别的. 2.查询空值解决办法:select * fro ...
- 操作ACCESS数据库注意事项
以下问题都是容易忽略,但却不容易找出问题的所在,让我头疼不少,故在此列出,即是一个总结,同样也给其他人参与! 1.使用参数形式执行SQL命令时,参数数组需与在SQL语句中参数名出现的位置及名称必须完全 ...
- 如何在Asp.net中备份Access数据库?
public void Create( string mdbPath ) { if( File.Exists(mdbPath) ) //检查数据库是否已存在 { thr ...
随机推荐
- 转 一些shell经验
http://www.cnblogs.com/xublogs/archive/2010/03/16/2292254.html http://www.cnblogs.com/stephen-liu74/ ...
- A Knight's Journey(dfs)
Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 25950 Accepted: 8853 Description Back ...
- hg 证书验证失败
hg clone https://bitbucket.org/pygame/pygame 出现abort: error: _ssl.c:504: error:14090086:SSL routines ...
- 【转】中兴G718C卡刷刷机教程(青漾2 4G)--不错
原文网址:http://www.zterom.com/guide/2278.html 刷机包 B11纯净版 适合长久使用_B11_lite_0130.zip 刷机用了20多分钟. 在和大家分享过中兴G ...
- i++和++i的老问题
对于++j,该式是指先将j的值自加1,然后再取j的值.自增过后参与计算 i的值也为10:对于i++,该表达式是指先取i的值做运算,再将i加1.参见过计算后再自增
- 综合查询员工和datetime.now和datetime.today区别
一:综合查询图 二:EmployeeListWindow.cs代码 using System; using System.Collections.Generic; using System.Compo ...
- Android 开机自启动
首先实现开机自启动: 第一步创建一个广播接收者,如MyBootBroadcastReceiver.java package com.example; import android.content.Br ...
- Xshell中文编码的设置
一直用的是SSH Secure Shell Client,关于中文乱码,一直找不到简便的解决方案,后来改用XShell,编码设置如下: 1.查看linux系统编码,linux命令: locale. 2 ...
- HDOJ(HDU) 2162 Add ‘em(求和)
Problem Description Write a program to determine the summation of several sets of integers. Input Th ...
- hdu 4424 & zoj 3659 Conquer a New Region (并查集 + 贪心)
Conquer a New Region Time Limit: 8000/4000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others ...