1.C#调用外部exe
// 引用库
using System.Diagnostics;
/*************************************************************
* 无参调用exe
*************************************************************/
// 具体调用代码
Process m_Process = new Process();
m_Process.StartInfo.FileName = "exePath"; //exe所在的文件夹绝对路径
m_Process.Start();
/*************************************************************
* 有参调用exe
*************************************************************/
Process cmd = new Process();
cmd.StartInfo.FileName = @"exePath"; //exe所在的文件夹绝对路径
cmd.StartInfo.CreateNoWindow = false;// 显示命令行窗口
cmd.StartInfo.WindowStyle = ProcessWindowStyle.Normal;
string param1 = "456", param2 = "789";
cmd.StartInfo.Arguments = param1+" "+param2;
cmd.Start();
参照链接
2.调用*.mdb文件(System.Data.OleDb)
2.1.调用*.mdb环境
a. 要使用【Microsoft Access 数据库引擎可再发行程序包】32位。
下载地址:Download Microsoft Access 2010 数据库引擎可再发行程序包 from Official Microsoft Download Centerbb
b.测试和打包时使用下x86环境运行
c.在NuGet中引入包"System.Data.OleDb"
2.2调用代码
// 引用包
using System.Data.OleDb;
//实现代码
private void bt_mdb_oledb_Click(object sender, EventArgs e)
{
OleDbConnection conn = new OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source="+数据库路径+";Jet OLEDB:Database Password="+ 数据库密码);
conn.Open();
OleDbCommand cmd = conn.CreateCommand();
cmd.CommandText = "select * from " + 表名; // 查询语句
OleDbDataReader dr = cmd.ExecuteReader(); // 执行查询语句
DataTable dt = new DataTable();
if (dr.HasRows) // 判断是否查询出结果
{
// 遍历取值
for (int i = 0; i < dr.FieldCount; i++)
{
dt.Columns.Add(dr.GetName(i));
}
dt.Rows.Clear();
}
// 关闭数据库(上面代码的最好写个try-catch,然后把这块放finally里面)
cmd.Dispose();
conn.Close();
}
//方法一**速度中等
OleDbDataReader reader = command.ExecuteReader();
while (reader.Read())
{
var t1 = reader[0];
}
//方法二**速度最慢
OleDbDataReader reader = command.ExecuteReader();
while (reader.Read())
{
var t1 = reader["字段名"];
}
//方法三**速度最快
OleDbDataReader reader = command.ExecuteReader();
while (reader.Read())
{
var t1 = reader.GetValue(0);
}