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

Passing connection string from linqpad to custom library causes login failure

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

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

SyncController syncController = new SyncController(this.Connection.ConnectionString);
syncController.SyncAll();

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

你是对的:连接打开后,密码将从连接字符串中消失。这是一个 feature of the .NET Framework,并不特定于 LINQPad。

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

string cxString = this.Connection.ConnectionString;

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

Persist Security Info=true

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