Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/jsp/3.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的连接_C#_Asp.net_Sql Server - Fatal编程技术网

C# 错误:无法打开到SQL Server的连接

C# 错误:无法打开到SQL Server的连接,c#,asp.net,sql-server,C#,Asp.net,Sql Server,我是Asp.Net的新手,现在我面临着这个问题。我已经尽可能多地尝试了从谷歌和这里得到的方法。但我仍然找不到解决办法。我曾尝试将数据源更改为127.0.0.1,但仍然出现相同的错误。当我使用MySql.Data.MySqlClient时,它实际上可以连接到数据库。但在使用System.Data.SqlClient时失败 你们都能帮我吗?多谢各位 代码隐藏: private DataTable GetData(string query) { DataTabl

我是Asp.Net的新手,现在我面临着这个问题。我已经尽可能多地尝试了从谷歌和这里得到的方法。但我仍然找不到解决办法。我曾尝试将数据源更改为127.0.0.1,但仍然出现相同的错误。当我使用MySql.Data.MySqlClient时,它实际上可以连接到数据库。但在使用System.Data.SqlClient时失败 你们都能帮我吗?多谢各位

代码隐藏:

private DataTable GetData(string query)
        {
            DataTable dt = new DataTable();
            string constr = ConfigurationManager.ConnectionStrings["WebAppConnStringSql"].ConnectionString;
            using (SqlConnection cons = new SqlConnection(constr))
            {           
                using (SqlCommand cmds = new SqlCommand(query))
                {
                    using (SqlDataAdapter sda = new SqlDataAdapter())
                    {
                        cmds.CommandType = CommandType.Text;
                        cmds.Connection = cons;
                        sda.SelectCommand = cmds;
                        **sda.Fill(dt);** //Error occurs here
                    }
                }
                return dt;
            }
Web.config

<connectionStrings>
    <add name="WebAppConnStringSql"
         connectionString="Data Source=localhost;Initial Catalog=vbsite;Integrated Security=True"
         providerName="System.Data.SqlClient"/>
  </connectionStrings>


您应该打开连接
SqlConnection.open()


错误信息非常清楚。您的数据库连接错误,或者服务器关闭,您没有访问权限,但是当我使用MySql.Data.MySqlClient连接到服务器时。没关系,没问题。数据库连接应该不是问题,因为我写了正确的信息?是的。我正在本地主机上做一个项目。通过使用visual studio和wampserver的phpmyadmin mysql数据库,您想连接到mysql数据库吗?System.Data.SqlClient用于连接到MS SQLServer。你需要是的,我也厌倦了。我得到的是相同的错误发生在cons.Open()行。@StudentABC尝试在连接字符串中将IntegratedSecurity设置为false。还是一样的错误。尝试使用和不使用cons.Open()加上集成安全性=False。还是一样的错误。
using (SqlConnection cons = new SqlConnection(constr))
{           
   cons.Open();
   using (SqlCommand cmds = new SqlCommand(query))
   {                   
      using (SqlDataAdapter sda = new SqlDataAdapter())
      {
         cmds.CommandType = CommandType.Text;
         cmds.Connection = cons;
         sda.SelectCommand = cmds;
         **sda.Fill(dt);** //Error occurs here
      }
   }
   return dt;
}