c# - 即使我们在密码前留有空格,Azure SQL 数据库也会连接

标签 c# sql-server asp.net-mvc azure azure-sql-database

我们在 MVC 中开发了 SQL 连接向导,用户将在其中输入 SQL Server 凭据,如果连接成功,我们将通过执行查询从数据库中提取记录。一切正常,但这里我们正在连接到 Windows Azure 数据库。

问题是密码是否为“test@123”并且我是否提供

      test@123

test@123         

从 UI 中连接成功并提取记录。

我们用来打开连接的代码是

string provider = "System.Data.SqlClient"; // for example
DbProviderFactory factory = DbProviderFactories.GetFactory(provider);

using (DbConnection conn = factory.CreateConnection())
{
     string connectionstring = FormatSqlConnection(SqlViewModel); // here we format sql conneciton string
     conn.ConnectionString = connectionstring;
     conn.Open();
     conn.Close();
     SqlViewModel.MessageType = MessageType.Success;
     SqlViewModel.MessageText = "Connection Succeeded..";
     SqlViewModel.SqlConnectionString = connectionstring;
 }

FormatSqlConnection 方法是

private static string FormatSqlConnection(SqlConnectionModel SqlViewModel)
{
    string connectionstring = string.Format("Data Source={0};User ID={1};Password={2};Initial Catalog={3};", SqlViewModel.Server, SqlViewModel.UserName, SqlViewModel.Password, SqlViewModel.DataBase);
    return connectionstring;
}

如何解决这个问题?

提前致谢

最佳答案

如果您不在值周围添加引号,我想这就是处理连接字符串的方式。

参见msdn

The basic format of a connection string includes a series of keyword/value pairs separated by semicolons. The equal sign (=) connects each keyword and its value. To include values that contain a semicolon, single-quote character, or double-quote character, the value must be enclosed in double quotation marks. If the value contains both a semicolon and a double-quote character, the value can be enclosed in single quotation marks. The single quotation mark is also useful if the value starts with a double-quote character.

Conversely, the double quotation mark can be used if the value starts with a single quotation mark. If the value contains both single-quote and double-quote characters, the quotation mark character used to enclose the value must be doubled every time it occurs within the value. To include preceding or trailing spaces in the string value, the value must be enclosed in either single quotation marks or double quotation marks. Any leading or trailing spaces around integer, Boolean, or enumerated values are ignored, even if enclosed in quotation marks. However, spaces within a string literal keyword or value are preserved. Single or double quotation marks may be used within a connection string without using delimiters (for example, Data Source= my'Server or Data Source= my"Server), unless a quotation mark character is the first or last character in the value.

所以您可以尝试将格式更改为

 string connectionstring = string.Format("Data Source={0};User ID={1};Password='{2}';Initial Catalog={3};", SqlViewModel.Server, SqlViewModel.UserName, SqlViewModel.Password, SqlViewModel.DataBase);

关于c# - 即使我们在密码前留有空格,Azure SQL 数据库也会连接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26800106/

相关文章:

c# - 失去焦点后在 ElementHost 的 WPF RichTextBox 中显示选择

sql-server - 为什么此查询会生成 PK 违规错误?

sql - Node.js SQL Server 代码似乎有误,但仍然有效?

asp.net-mvc - 如何让 .Net Core Action 返回 Nothing 或 Redirect

c# - 如何传递要包含在 URL 中的参数?

jquery - ajax 请求中出现未经授权的结果

c# - 如果连接在 .NET 远程服务器上的远程调用中途终止,会发生什么情况?

c# - 无法先投代码

c# - Linq Contains() 是否检查 HashSet?

c# - 在 Linq/ODataController 中使用全文搜索