请问为什么c#打开sqlserver数据库失败?代码如图

请问为什么c#打开sqlserver数据库失败?代码如图图片

连接本机应该是
Server=.;

你的缺少了等号。

看你的代码,最可能出问题的是连接字符串,看下你的的数据库是否使用集成验证,以及是否允许远程连接。

数据库的连接字符串有问题,
如下是.NET 类库中数据库连接源代码,你可以参照一下格式和字符串
/// /// 可以接受三种格式的数据库连接字符串
/// 1. 服务名称=(local);数据库名称=EDNSM;用户名称=sa;用户密码=123456
/// 2. Data Source=(local);Initial Catalog=EDNSM;User ID=sa;Password=123456
/// 3. server=(local);uid=sa;pwd=;
///
///
public DatabaseInfo(string connectionString)
{
#region 服务器名

        this.server = this.GetSubItemValue(connectionString, "服务名称");
        if (this.server == null)
        {
            this.server = this.GetSubItemValue(connectionString, "Data Source");
        }
        if (this.server == null)
        {
            this.server = this.GetSubItemValue(connectionString, "server");
        }

        #endregion

        #region 数据库名

        this.database = this.GetSubItemValue(connectionString, "数据库名称");
        if (this.database == null)
        {
            this.database = this.GetSubItemValue(connectionString, "Initial Catalog");
        }
        if (this.database == null)
        {
            this.database = this.GetSubItemValue(connectionString, "database");
        }

        #endregion

        #region 用户名称

        this.userID = this.GetSubItemValue(connectionString, "用户名称");
        if (this.userID == null)
        {
            this.userID = this.GetSubItemValue(connectionString, "User ID");
        }
        if (this.userID == null)
        {
            this.userID = this.GetSubItemValue(connectionString, "uid");
        }

        #endregion

        #region 用户密码

        this.password = this.GetSubItemValue(connectionString, "用户密码");
        if (this.password == null)
        {
            this.password = this.GetSubItemValue(connectionString, "Password");
        }
        if (this.password == null)
        {
            this.password = this.GetSubItemValue(connectionString, "pwd");
        }

        #endregion
    }