简体   繁体   English

当我可以使用具有相同登录名的SQL Server Management Studio进行连接时,为什么我的应用程序不连接到SQL Server数据库?

[英]Why won't my application connect to SQL Server database when I can connect using SQL Server Management Studio with the same login?

I realize this may be a difficult (impossible?) question to answer since I can't provide access to the actual server involved, but maybe there's something obvious I'm missing. 我意识到这可能是一个很难回答的问题(因为不可能),因为我无法提供对涉及的实际服务器的访问权限,但是也许我显然缺少了一些东西。

I have a C# Windows Forms application (.Net 4.5) that needs to connect to a SQL Server 2012 database. 我有一个C#Windows Forms应用程序(.Net 4.5),需要连接到SQL Server 2012数据库。 The connection string in App.config is as follows: App.config的连接字符串如下:

<connectionStrings>
  <add name="MyConnStr"
    connectionString="server=SERVERNAME.ads.lcl;database=MyDatabase;uid=MyUser;password=abcdefg1234567"
    providerName="AspNetSqlProvider" />

I am able to connect to and read from the database with SQL Server Management Studio using the same login and password (with SQL Server Authentication), so I know I have the server, database, uid, and password correctly specified in the connection string. 我能够使用相同的登录名和密码(通过SQL Server身份验证)使用SQL Server Management Studio连接到数据库并从中读取数据库,因此我知道在连接字符串中正确指定了服务器,数据库,uid和密码。 But when I debug my program, I get an SQLException saying Cannot open database "MyDatabase" requested by the login. The login failed. Login failed for user 'MyUser'. 但是,当我调试程序时,出现SQLException,提示Cannot open database "MyDatabase" requested by the login. The login failed. Login failed for user 'MyUser'. Cannot open database "MyDatabase" requested by the login. The login failed. Login failed for user 'MyUser'.

I've done this a thousand times before, so I'm at a loss. 我已经做过一千遍了,所以我很茫然。

Here are the relevant sections of code, if it helps at all: 如果有帮助,请参见以下相关代码部分:

public static string MyConnStr
{
  get { return ConfigurationManager.ConnectionStrings["MyConnStr"].ConnectionString; }
}

public static int[] GetIntArrayFromDB(string query)
{
  DataTable dt = new DataTable();

  using (SqlConnection conn = new SqlConnection(MyConnStr))
  {
    using (SqlCommand cmd = new SqlCommand(query, conn))
    {
      using (SqlDataAdapter da = new SqlDataAdapter(cmd))
      {
        try
        {
          conn.Open();
          da.Fill(dt);
          conn.Close();
        }
        catch (SqlException)
        {
          return null;
        }
        finally
        {
          if (conn != null)
            conn.Dispose();
        }
      }
    }
  }

  int numRows = dt.Rows.Count;
  int[] retInt = new int[numRows];
  for (int i = 0; i < numRows; i++)
  {
    retInt[i] = Convert.ToInt32(dt.Rows[i][0]);
  }

  return retInt;
}

The query being passed to GetIntArrayFromDB() is "SELECT myID FROM myTable" . 传递给GetIntArrayFromDB()的查询是"SELECT myID FROM myTable" Any ideas? 有任何想法吗?

UPDATE : The approach above with the named connection string is perfectly valid and works, if you don't misspell the connection string name in the config file. 更新 :如果您在配置文件中没有错误拼写连接字符串的名称,则上述带有命名连接字符串的方法完全有效且可行。 Sorry for this, I'm working on a sleep deficit. 抱歉,我正在处理睡眠不足。 Thanks to everyone who offered suggestions! 感谢所有提供建议的人!

I fixed it by changing the connection string specified in the App.config file to the following (note that the format of the connection string itself is the same, only the name is different): 我通过将App.config文件中指定的连接字符串更改为以下内容来修复它(请注意,连接字符串本身的格式相同,只是名称不同):

<connectionStrings>
  <remove name="LocalSqlServer" />
  <add name="LocalSqlServer" connectionString="server=MyServer.ads.lcl;database=MyDatabase;uid=MyUser;password=abcdefg1234567"
    providerName="AspNetSqlProvider" />
</connectionStrings>

and by referencing the connection string in my C# code this way: 并通过这种方式在我的C#代码中引用连接字符串:

get
{
  return ConfigurationManager.ConnectionStrings[0].ConnectionString;
}

I do NOT understand why this made any difference! 明白为什么这会有所不同! It seems quite bizarre. 看起来很奇怪。

If this is SQL Server, then your connection string should be something like the following 如果这是SQL Server,则您的连接字符串应类似于以下内容

<add name="ConnectionStringName"
 connectionString="Data Source=DatabaseServer; Initial Catalog=DatabaseName; User ID=UserName; Password=YourPassword;"
 providerName="System.Data.SqlClient" />

Not sure if your example just isn't using the "Data Source" and "Initial Catalog" entries, but it looks like you're using "server" and "database" instead. 不知道您的示例是否不使用“数据源”和“初始目录”条目,但是看起来您正在使用“服务器”和“数据库”。

According to connectionstrings.com , your connection string should be in this format for SQL 2012: 根据connectionstrings.com ,对于SQL 2012,您的连接字符串应采用以下格式:

Server=myServerAddress;Database=myDataBase;User Id=myUsername; Server = myServerAddress;数据库= myDataBase;用户ID = myUsername; Password=myPassword; 密码= MYPASSWORD;

I've never used the format you are using, give this a shot. 我从未使用过您使用的格式,请尝试一下。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在Visual Studio中创建安装文件时,为什么不能连接到SQL Server数据库? - Why can't I connect to the SQL Server database when I create setup file in Visual studio? 无法连接到我的SQL Server数据库? - Can't connect to my SQL Server database? SQL Server Management Studio无法连接 - SQL Server Management Studio can not connect 我无法连接到本地SQL Server数据库 - I can't connect to my local SQL Server database 我无法从Visual Studio 2012连接到我的SQL Server 2014数据库 - I can't connect to my SQL Server 2014 database from Visual Studio 2012 为什么我的visual studio 2015 SQL Server无法与Azure SQL数据库连接? - Why my visual studio 2015 SQL Server doesn't connect with Azure SQL Database? 如何在SQL Server Management Studio中使用Visual Studio创建连接数据库? - How connect database created in SQL Server Management Studio with Visual Studio? 无法连接到SQL Server数据库:用户&#39;sa&#39;的登录失败 - Can't connect to SQL Server database: Login failed for user 'sa'' 无法将Visual Studio连接到SQL Server数据库 - Can't connect Visual Studio to SQL Server database 应用程序无法连接到其他计算机上的SQL Server数据库副本 - Application can't connect to copy of SQL Server database on other computer
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM