将 VBScript 转换为 C# [英] Convert a VBScript to C#

查看:52
本文介绍了将 VBScript 转换为 C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人可以帮助我将此脚本转换为 C# 我仍然是 C# 的初学者和学习者.该脚本使用 OpenCurrentDatabase 打开(并保持打开)一个 Access .mdb 文件,我必须使用此方法和代码,但已转换为 C#,非常感谢任何帮助.

Can some one help me converting this script to C# I am still very much a begiiner and learning in C#. The script uses OpenCurrentDatabase to open (and keep open) an Access .mdb file, I must use this method and the code as it is but converted to C#, any help would be very much appreciated.

我使用记事本编辑 cs 文件并使用 csc.exe 编译它(我没有任何其他 C# 工具).

I’m using notepad to edit the cs file and csc.exe to compile it (I don’t have any other C# tools).

dim fso    
Set fso = CreateObject("Scripting.FileSystemObject")

dim filePath
filePath = fso.GetParentFolderName(WScript.ScriptFullName)
filePath = filePath & "\test.mdb"
'WScript.Echo filePath

If (fso.FileExists(filePath)) Then
    dim appAccess
    set appAccess = createObject("Access.Application")
    appAccess.visible = true
    appAccess.UserControl = true 'To leave the application open after the script completes you need to set the UserControl property to true.
    appAccess.OpenCurrentDatabase filePath, True, "mypassword"
Else
    WScript.Echo "File doesn’t exist"
End If

推荐答案

如果您将 VBScript 保存在某个地方,您可以使用

If you have your VBScript saved somewhere you can call it from within C# with

System.Diagnostics.Process.Start(@"P:\ath\to\Script.vbs");

如果你只需要打开一个到数据库的连接:

If you just need to open a connection to the database:

// Declare the path to the database file
var filePath = @"Path\to\database.accdb";

// If statement using File.Exists()
if (File.Exists(filePath))
{ 
    // Create new OleDbConnection object and call Open()
    var conn = new OleDbConnection($"Provider=Microsoft.ACE.OLEDB.12.0;Data Source={filePath}");
    conn.Open();
}
else
{
    // Write to the console if the file does not exist
    Console.WriteLine("The file does not exist");
}

您将需要引用 System.Data.OleDb 以使用 OleDbConnection,而 System.IO 用于 File.Exists()

You will need to reference System.Data.OleDb to use OleDbConnection, and System.IO for File.Exists()

希望能帮到你

这篇关于将 VBScript 转换为 C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆