将连接字符串从 linqpad 传递到自定义库会导致登录失败

col*_*dbh 4 c# dll connection-string linqpad

我正在编写一个 linqpad 脚本,用于将数据从 excel 文件迁移到数据库中。数据导入后,需要在多个数据库之间同步。这是在 dll 中完成的,因为该功能在其他地方使用。

我的问题是,当从 linqpad 将连接字符串传递给我的 dll 时,我收到消息 Login failed for user 'xxxxxxx'。据我所知,这是因为 SQL 身份验证的密码未在连接字符串中传递。有什么办法可以让 linqpad 在连接字符串中包含密码:

SyncController syncController = new SyncController(this.Connection.ConnectionString);
syncController.SyncAll();
Run Code Online (Sandbox Code Playgroud)

如果可能的话,我想避免强迫运行它的人在运行它时更新密码变量。谢谢。

Joe*_*ari 5

您说得对:打开连接后,密码将从连接字符串中消失。这是.NET Framework 的一项功能,并非特定于 LINQPad。

有两种解决方法。首先,在查询任何数据之前,您可以捕获连接字符串作为 LINQPad 脚本中的第一行:

string cxString = this.Connection.ConnectionString;
Run Code Online (Sandbox Code Playgroud)

另一种解决方法是将以下文本添加到您的连接字符串中:

Persist Security Info=true
Run Code Online (Sandbox Code Playgroud)

要在 LINQPad 中执行此操作,请右键单击该连接并选择Properties。单击Advanced,然后在提供的框中输入附加连接字符串文本。