无法连接到数据库 [英] Can't connect to database

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

问题描述

我已经从服务器资源管理器向我的项目添加了数据库 - >数据连接 - >添加连接。它创建数据库没有任何错误但是当我尝试连接到数据库时,它给出了很长的错误 -

System.Data.dll中发生类型'System.Data.SqlClient.SqlException'的异常但未在用户代码中处理



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

我无法理解有什么错误。请帮我解决这个问题。



以下是代码。我在连接的数据库中创建一个表。

I have added database to my project from server explorer -> Data connections -> Add connection. It creates database without any error but when I try to connect to the database, It gives very long error -
An exception of type 'System.Data.SqlClient.SqlException' occurred in System.Data.dll but was not handled in user code

Additional information: 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)
I can't understand what is the error about. Please help me to solve this.

Following is the code. I am creating a table in the connected database.

namespace OnlineReportSystem.Admin
{
    public partial class AdminRagistration : System.Web.UI.Page
    {
        SqlConnection con = new SqlConnection("Data Source=(LocalDB)\v11.0;AttachDbFilename=C:\\Users\\Avani\\Documents\\\\\\mynewdb.mdf;Integrated Security=True;Connect Timeout=30;");
        protected void Page_Load(object sender, EventArgs e)
        {
            
        }

        protected void btnNext1_Click(object sender, EventArgs e)
        {
           // Response.Write(TabContainer1.ActiveTabIndex.ToString());
            TabContainer1.ActiveTab = TabContainer1.Tabs[TabContainer1.ActiveTabIndex + 1];
        }

        protected void btnCreateSchema_Click(object sender, EventArgs e)
        {
            SqlCommand cmd = new SqlCommand();
            cmd.CommandText = "Create table " + txtSchema.Text + ";";
            cmd.CommandType = CommandType.Text;
            try
            {
                using (con)
                {
                    con.Open();
                    cmd.ExecuteNonQuery();
                    con.Close();
                    Response.Write("successfull");
                }
            }
            catch (Exception ex)
            {
                Response.Write(ex);
            }
        }
    }
}

推荐答案

仔细检查。

查看您通过Server explorer在VS中建立的连接并确保其正常工作。

如果是,请在服务器资源管理器窗格中突出显示您的数据库,然后查看属性窗格。将显示连接字符串的工作示例,您可以将其复制并粘贴到应用程序或配置文件中。如果连接字符串直接在VS中工作,那么它也可以在你的应用程序中工作,只要它没有改变。
Double check it.
Look at the connection you established in VS via Server explorer and make sure it works.
If it does, highlight your database in the Server Explorer pane, and look at the Properties pane. A working example of the connection string will be shown, which you can copy and paste into your app or config file. If the connection string works directly in VS, then it'll work in your app as well, provided it's unchanged.


你的SQL连接存在一些问题,你正在制作..

再次尝试使用Fresh。选择数据源并在添加连接对话框中单击继续后,填写服务器名称并选择数据库名称。在左下角有一个测试连接。单击此测试连接按钮以检查是否可以连接到数据库。如果不是..



您需要检查以下内容。

a。转到服务并确保SQL Server服务正在运行

b。如果是命名实例,请确保SQL Server浏览器服务正在运行

c。确保SQL Server配置为允许远程连接

d。检查SQL Server错误日志,查看确认SQL正在侦听预期网络接口和端口的消息

e。使用来自客户端计算机的PING测试服务器连接

f。使用TELNET或PowerShell测试端口连接到客户端计算机的服务器和端口(从步骤4开始)。

例如

a。 TELNET< server-name> 1433

b。 PowerShell:1433 | %{echo((new-object Net.Sockets.TcpClient).Connect(YourServerName,
there's some problem with your SQL connection, which you are making..
Try this Fresh again.. After you choose your data Source and click continue on the Add connection dialog box you filling the Server name and select the Database name . On the bottom left there is a Test connection. Click on this Test Connection button to check if you can connect to Database. If not..

You need check the following .
a. Go to Services and Make sure SQL Server Service is running
b. If a named instance, make sure SQL Server browser service is running
c. Make sure SQL Server is configured to allow remote connections
d. Examine the SQL Server error log for messages confirming that SQL is listening on the expected network interfaces and ports
e. Test server connectivity with PING from the client machine
f. Test port connectivity using TELNET or PowerShell to the server and port (from step 4) from the client machine.
For example
a. TELNET <server-name> 1433
b. PowerShell: 1433 | % { echo ((new-object Net.Sockets.TcpClient).Connect("YourServerName",


_))服务器侦听TCP端口
_)) "server listening on TCP port


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

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