c# - 我在连接 SQL Server 时遇到异常

标签 c# mysql

    private void frmResibo_Load(object sender, EventArgs e)
    {

        market m = new market();
        string cs = @"Data Source =.localhost; Initial Catalog = thesisdb; User ID=root; Integrated Security = true";
        SqlConnection cn = new SqlConnection(cs);
        SqlDataAdapter da = new SqlDataAdapter("select * from tbl_itemlist", cn);
        da.Fill(m, m.Tables[0].TableName);



         ReportDataSource rds = new ReportDataSource("tbl_itemlist", m.Tables[0]);
         this.reportViewer1.LocalReport.DataSources.Clear();
         this.reportViewer1.LocalReport.DataSources.Add(rds);
         this.reportViewer1.LocalReport.Refresh();

         this.reportViewer1.RefreshReport();

    }

我在“da.Fill---”行中遇到了这样的错误

System.Data.SqlClient.SqlException: 'A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: Named Pipes Provider, error: 40 - Could not open a connection to SQL Server)'

最佳答案

连接字符串指定“Data Source =.localhost”;通常您可以输入“Data Source = .”或“Data Source = localhost”(两者含义相同:运行它的计算机)。但是,由于 localhost 前面有一个点,它可能会尝试查找名为“.localhost”的计算机,这可以解释为什么它无法连接到该 SQL 服务器。

另请注意,您的 SQL 连接和数据适配器是一次性的,因此应位于“using” block 中。

关于c# - 我在连接 SQL Server 时遇到异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48963197/

相关文章:

c# - 使用 SqlDataReader 时,如何使来自 BLOB 的流在普通旧 C# 对象中可用?

c# - 如何使 WPF 数据网格的第一行成为自动添加新行的行

c# - 如何删除未选择的 TreeView 节点

c# - 如何在 C# 中使用 SMO 使用 FILE STREAM 备份和恢复数据库

c# - 编写 AutoMapper 代码最有效的方法是什么?

php - 在单个查询中合并到两个表以对数据进行排序

mysql - 我的表是什么 ROW_FORMAT?

php - 将 Symfony2 项目从本地主机移动到开发服务器

mysql - 我该如何优化这个查询?

MySql - 在 Windows 中使用 --with-debug 启用存储过程功能访问