数据库连接问题 [英] Database Connection Issue

查看:61
本文介绍了数据库连接问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我收到一段链接到数据库的代码的错误提示:

建立与SQL Server的连接时发生与网络相关或特定于实例的错误.服务器未找到或无法访问.验证实例名称正确,并且已将SQL Server配置为允许远程连接. (提供者:命名管道提供者,错误:40-无法打开与SQL Server的连接)

我的代码是:

Hi, I am recieving the follwing error on a piece of code linking to a database:

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)

My code is:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Data.OleDb;
using System.Data;
using System.Data.SqlClient;

namespace WpfApplication1
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {


        public MainWindow()
        {
            InitializeComponent();
        }

      
        public void dbCC(){
        // 1. Instantiate the connection
        SqlConnection conn = new SqlConnection(
            "Data Source=(local);Initial Catalog=Northwind");

        SqlDataReader rdr = null;

        try
        {
            // 2. Open the connection
            conn.Open();

            // 3. Pass the connection to a command object
            SqlCommand cmd = new SqlCommand("select * from Customers", conn);

            //
            // 4. Use the connection
            //

            // get query results
            rdr = cmd.ExecuteReader();

            // print the CustomerID of each record
            while (rdr.Read())
            {
                Console.WriteLine(rdr[0]);
            }
        }
        finally
        {
            // close the reader
            if (rdr != null)
            {
                rdr.Close();
            }

            // 5. Close the connection
            if (conn != null)
            {
                conn.Close();
            }
        }
    }

        private void button3_Click(object sender, RoutedEventArgs e)
        {
            dbCC();
        }



    }
}



这是我第一次在C#中使用数据库,显然这只是连接到db并检索一些数据的简单测试,但是我似乎陷入了第一个障碍,我不知道为什么!!

任何帮助将不胜感激!



This is my first time using a database in c#, this is obviously just a simple test to connect to a db and retrieve some data, but I seem to be stuck at the first hurdle and I haev no idea why!!

Any help would be appreciated!

推荐答案

在这里,检查一下这些束

解决SQL连接问题的步骤 [ ^ ]

SQL Server 2005连接性问题疑难解答-第一部分 [对SQL Server 2005中的连接问题进行故障排除-第二部分 [ ^ ]

对SQL Server 2005中的连接问题进行故障排除-第III部分 [ ^ ]

顺便说一句,我也给了你这堆东西,以防将来出错.
Here you go, check these bunch

Steps to troubleshoot SQL connectivity issues[^]

SQL Server 2005 Connectivity Issue Troubleshoot - Part I[^]

Troubleshoot Connectivity Issue in SQL Server 2005 - Part II[^]

Troubleshoot Connectivity Issue in SQL Server 2005 - Part III[^]

BTW I gave you this bunch also for future errors.


每次在计算机上安装SQL Server时,我都会遇到这个问题.默认安装禁用了远程连接",并且必须在主机上手动启用此功能.该方法根据您所运行的SQL Server的版本而有所不同,因为工具会随着版本的不同而不断变化.但是我敢肯定,使用Google可以找到正确的说明.

此外,如果您打算动态定位并选择网络上的特定实例,则需要打开SQL Server Browser服务.默认情况下,该功能也是禁用的,没有它,您的应用程序将无法发现它.
I run into this one every time I install SQL Server on a machine. The default installation has Remote Connections disabled, and this function has to be manually enabled on the host machine. The method varies depending on the version of SQL Server you''re running, because the tools keep changing with each version. But I''m sure you''ll have no trouble finding the right instructions using Google.

Additionally, if you''re planning to dynamically locate and select a specific instance on a network, you''ll want to turn on the SQL Server Browser service. That, too, is disabled by default, and without it your application won''t be able to discover it.


尝试验证数据源的实际IP地址/计算机名称.如果您使用的是SQL Server Express Edition,则通常数据源可能是[IP地址] \ SQLEXPRESS.尝试首先使用SQL Server Management Studio进行验证.另外,您可能希望在连接字符串上输入用户名(如果需要,还需要输入密码).这可能是您无法连接到数据库的原因之一.
Try to verify the actual IP address/Computer name of the data source. If you are using SQL Server Express Edition, usually, the data source could be [IP Address]\SQLEXPRESS. Try to verify it first using SQL Server Management Studio. Also, you might want putting a user name (and password if required) on your connection string. It might be one of the reasons why you can''t connect to your database.


这篇关于数据库连接问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆