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

上一篇:CentOS+Nginx+PHP+MySQL详细配置(图解)


下一篇:Centos7_OpenSSH离线升级8.6和OpenSSL1.0.2u