如何在另一个PC中访问Sql Server? [英] How Can I Access Sql Server In Another Pc?

查看:132
本文介绍了如何在另一个PC中访问Sql Server?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个应用程序。我也有网络连接的机器。

所以我想从另一台网络机器访问数据库。



所以我创建了一个应用程序,我想访问另一台电脑?



所以我该怎么做?



< pre lang =cs> string connetionString = null ;

// string sql = null;
string name = ;
string pass = ;

connetionString = 数据源= 172.21.1.91,1433;初始目录= EnqInfo;集成安全=真;


使用(SqlConnection con = new SqlConnection(connetionString) )
{
//
// 打开SqlConnection。
//
con.Open();
//
// 以下代码使用基于SqlConnection的SqlCommand。
// < span class =code-comment>

使用(SqlCommand command = new SqlCommand( SELECT * FROM Login WHERE U_Name = @ Usr,con))
{

command.Parameters.AddWithValue( @ Usr,UName。文本);
SqlDataReader reader = command.ExecuteReader();
while (reader.Read())
{
name = reader.GetString( 1 ); // 名称字符串
pass = reader.GetString( 2 ); // 密码字符串
}

if (name == UName.Text&& pass == PWord.Text)
{

ClassEnq.Uname = UName.Text;
MessageBox.Show( 密码已接受);
this .Close();
setEnableToolStripMenuItem( true );
setEnableToolStripMenuItem1( true );
setDisableToolStripMenuItem( false );

}

else
{
MessageBox.Show( 密码不正确,请重新输入密码);
setEnableToolStripMenuItem( false );
setEnableToolStripMenuItem1( false );
}
}







所以我分配了我的IP地址和端口号码到数据源。



但它给我一个错误说,



未处理的异常发生在你的申请。如果你点击继续,应用程序将忽略此错误并尝试继续......等等




网络相关或在建立与sql server的连接时发生特定于实例的错误。服务器未被发现或无法接受。



当我将数据源更改为我的电脑名称时,它只能在我的电脑上运行。



那怎么办呢?

解决方案

连接失败的可能性很多,例如:

- SQL Server未在目标计算机上运行

- 防火墙阻止通信等等。



这里有几个相当不错的列表要检查的内容:

- http:// www。 sswug.org/articlesection/default.aspx?TargetID=44331 [ ^ ]



- https://social.msdn.microsoft.com/Forums/sqlserver/en-US/b43775b9-cd96-4f14-bd1e-b40172c5fc83/a-networkrelated-or-instancespecific-error-occurred-while -stablishing-a-connection-to-sql-server?forum = sqlsecurity [ ^ ]


根据您的问题,这意味着您需要从您的应用程序中使用来自其他网络电脑的sql server。因为您应该更改 ConnectionString 并在SQL服务器SUrface区域进行一些配置以进行远程连接。



请点击链接:



http://searchsqlserver.techtarget.com/tip/Using-Surface-Area-Configuration-to-lock-down-SQL-Server [ ^ ]



http://www.scrumdesk.com/Articles/HowToEnableSQLServerRemoteConnections.html [ ^ ]


需要更改两个级别的安全性才能允许远程acc ess。



SQL Server配置。默认情况下,在Express,Developer和Enteprise Evaluation版本中,禁用通过TCP / IP协议进行连接。使用SQL Server配置管理器启用此功能。



Windows防火墙。虽然完全禁用防火墙将适用于此组件,但这样做并不是安全性最佳实践(也不是必需的)。 (注意:在本节中,我假设一个默认配置。有许多设置可以更改,这会稍微影响这些步骤。)



有两种情况取决于您要连接的SQL Server实例的类型:



默认实例(仅通过计算机名连接)。在TCP端口1433或数据库引擎服务上添加允许传入规则。



命名实例(按计算机名称+实例名称连接)。在UDP端口1434上添加允许传入规则以访问SQL浏览器服务。在数据库引擎服务上添加允许传入规则。


i have created an application. and i have network linked machines as well.
so i want to access the database from another network machine.

so i have created an application and i want to access in another pc?

so how can i do that?

string connetionString = null;

            //string sql = null;
            string name = "";
            string pass = "";

            connetionString = "Data Source=172.21.1.91,1433;Initial Catalog=EnqInfo;Integrated Security=True";


            using (SqlConnection con = new SqlConnection(connetionString))
            {
                //
                // Open the SqlConnection.
                //
                con.Open();
                //
                // The following code uses an SqlCommand based on the SqlConnection.
                //
                using (SqlCommand command = new SqlCommand("SELECT * FROM Login WHERE U_Name=@Usr", con))
                    {

                        command.Parameters.AddWithValue("@Usr", UName.Text);
                        SqlDataReader reader = command.ExecuteReader();
                        while (reader.Read())
                        {
                            name = reader.GetString(1);  // Name string
                            pass = reader.GetString(2); // Password string
                        }

                        if (name == UName.Text && pass == PWord.Text)
                        {

                            ClassEnq.Uname = UName.Text;
                            MessageBox.Show("Password Accepted");
                            this.Close();
                            setEnableToolStripMenuItem(true);
                            setEnableToolStripMenuItem1(true);
                            setDisableToolStripMenuItem(false);

                        }

                        else
                        {
                            MessageBox.Show("Password Incorrect, Please Re-Enter Your Password");
                            setEnableToolStripMenuItem(false);
                            setEnableToolStripMenuItem1(false);
                        }
                    }




so i have assigned my ip address and port number to data source.

but it gives me an error saying,

unhandled exception has occurred in your application. if you click continue, the application will ignore this error and attemp to continue...... etc


A network related or instance-specific error occured while establishing a connection to sql server. the server was not found or was not accecible.

when i change datasource to my pc name, it will only work on my pc only.

so how can do that?

解决方案

There are lot of possibilities why the connection may fail, for example:
- the SQL Server is not running on the target computer
- firewall is blocking the communication and so on.

Here's few quite good lists what to check:
- http://www.sswug.org/articlesection/default.aspx?TargetID=44331[^]

- https://social.msdn.microsoft.com/Forums/sqlserver/en-US/b43775b9-cd96-4f14-bd1e-b40172c5fc83/a-networkrelated-or-instancespecific-error-occurred-while-establishing-a-connection-to-sql-server?forum=sqlsecurity[^]


As per your question it means that you need to use sql server from another network pc from your application. For you should change ConnectionString and make some configuration on SQL server SUrface Area for remote connection.

Please follow the links:

http://searchsqlserver.techtarget.com/tip/Using-Surface-Area-Configuration-to-lock-down-SQL-Server[^]

http://www.scrumdesk.com/Articles/HowToEnableSQLServerRemoteConnections.html[^]


There are two levels of security that need to be changed in order to allow remote access.

SQL Server configuration. By default in Express, Developer, and Enteprise Evaluation editions, connecting by the TCP/IP protocol is disabled. Enable this using SQL Server Configuration Manager.

Windows Firewall. While disabling the firewall entirely will work for this component, doing so is not a security best-practice (nor is it required). (Note: in this section, I assume a default configuration. There are many settings that can be changed which affect these steps slightly.)

There are two cases depending on the type of SQL Server instance you're connecting to:

Default instance (connect by computer name only). Add an allow incoming rule either on TCP port 1433 or the database engine service.

Named instance (connect by computer name + instance name). Add an allow incoming rule on UDP port 1434 to access to the SQL Browser service. Add an allow incoming rule on the database engine service.


这篇关于如何在另一个PC中访问Sql Server?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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