gpt4 book ai didi

c# - 从外部文件读取连接字符串

转载 作者:太空宇宙 更新时间:2023-11-03 10:25:30 25 4
gpt4 key购买 nike

我对 C# 和 SQL 都不熟悉。我试图在程序加载时从外部 .text 文件读取连接字符串,然后在代码中需要时将其用作变量,但是我遇到了以前从未见过的错误。可能出了什么问题?

连接字符串是这样的:

@"Data Source=.\wintouch;Initial Catalog=bbl;User ID=sa;Password=Pa$$w0rd";

我用来将其转换为字符串的代码是这样的:

    private void Form1_Load_1(object sender, EventArgs e)
{

string connectionString;
var path = @"C:\Users\Administrator\Desktop\connstring.txt";
using (StreamReader sr = new StreamReader(path))
{
connectionString = sr.ReadLine();
}

var connection = new SqlConnection(connectionString);


SqlConnection conn = connection;

但是,正如我提到的,我遇到了这个错误:

An unhandled exception of type 'System.ArgumentException' occurred in System.Data.dll

Additional information: Keyword not supported: '@"data source'.

最佳答案

从文本文件中取出 @ 符号和双引号。不需要这些。

您的文本文件应为:

Data Source=.\wintouch;Initial Catalog=bbl;User ID=sa;Password=Pa$$w0rd;

重要提示

您不应将您的用户凭据(尤其是 sa!!)作为纯文本存储在文件中。

关于c# - 从外部文件读取连接字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31726891/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com