Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/71.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/actionscript-3/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 将SQL Server 2012数据库连接到Visual Studio 2012_C#_Sql_Visual Studio_Sql Server 2012 - Fatal编程技术网

C# 将SQL Server 2012数据库连接到Visual Studio 2012

C# 将SQL Server 2012数据库连接到Visual Studio 2012,c#,sql,visual-studio,sql-server-2012,C#,Sql,Visual Studio,Sql Server 2012,我正在尝试使用SQL Server 2012将visual studio中的项目连接到数据库。这是我的代码: using System; using System.Collections.Generic; using System.Linq; using System.Data; using System.Text; using System.Windows.Forms; using System.Data.SqlClient; namespace CollegeManagementSyste

我正在尝试使用SQL Server 2012将visual studio中的项目连接到数据库。这是我的代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Data;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace CollegeManagementSystem.lib
{
public class DatabaseConnection
{
    public static SqlConnection Con;

    public static SqlConnection GlobalConnection()
    {
        string ConString = @"Data Source=DESKTOP-JTVC4I1\\SQLEXPRESS;" + Global.ServerName + ";Initial Catalog=College" + Global.Database + ";Integrated Security=True;User Id=sa" + Global.SUserId + ";Password=123" + Global.SPassword + ";Connection Timeout=1000";
        Con = new SqlConnection();
        Con.ConnectionString = ConString;
        if (Con.State == ConnectionState.Open)
        {
            Con.Close();
        }
        Con.Open();
        return Con;
    }
我的数据库的名称是college.db,我也很困惑服务器中有一个名为master.db的数据库,它与我在数据库college.db中创建的表相同,但我没有创建它

我得到了一个错误:

发生与网络相关或特定于实例的错误时 正在建立与SQL Server的连接。找不到服务器,或者 无法访问。验证实例名称是否正确,以及 SQL Server配置为允许远程连接


我正在本地运行SQL Server。如何修复此问题?

如下图所示更改您的连接字符串。因为Global.ServerName已经有了服务器名,但在您的代码中该名称会重复。我还认为Global.Database、Global.SUserId、Global.SPassword是重复的

string ConString = @"Data Source=" + Global.ServerName + 
        ";Initial Catalog=" + Global.Database + 
        ";Integrated Security=False;User Id=" + Global.SUserId + 
        ";Password=" + Global.SPassword + ";Connection Timeout=1000";
你可以试试这个:

string ConString = "Server=DESKTOP-JTVC4I1\\SQLEXPRESS;Database=College;User Id=sa;Password=123;"
还要确保数据库名称正确无误。在“说明”中,您说您的数据库名称是college,但在连接字符串中您使用了college。

您的连接字符串的Integrated Security=True。这意味着您指定的用户Id和密码属性将被忽略,并且将使用当前记录的Windows标识

尝试将连接字符串更改为integratedsecurity=False

更多详情请访问

如果为false,则在连接中指定用户ID和密码。如果为true,则当前Windows帐户凭据用于身份验证。 已识别的值为true、false、yes、no,强烈建议使用sspi,这相当于true。 如果指定了用户ID和密码,并且集成安全性设置为true,则将忽略用户ID和密码,并使用集成安全性


Global.ServerName的值是多少?公共静态字符串ServerName=DESKTOP-JTVC4I1\\SQLEXPRESS;设置一个断点并查看ConnString的值。我想你会知道问题出在哪里的。数据库工作正常。但是,当我尝试登录时,提示用户名和密码不正确,用户名和密码都称为admin。您尝试过我更新的答案吗?实际的用户名和密码是什么?Global.SUserId和Global.SPassword的值是多少。两者是否相同?是的,我尝试了您的解决方案,Global.SUserId和Global.SPassword的值也是admin,SQL server中用户名密码的实际值是多少?Remove Integrated Security=True