Click here to Skip to main content
15,886,037 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
See more:
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?

C#
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?
Posted
Comments
PIEBALDconsult 18-Jan-15 23:22pm    
You probably need to open a port in your firewall.
Here are some similar questions:
http://www.codeproject.com/Questions/867031/Login-to-SQL-Server-R-remotely
http://www.codeproject.com/Questions/863761/Connecting-to-SQL-SERVER
http://www.codeproject.com/Answers/855504/How-to-access-database-from-another-PC#answer2
Hemas Ilearn 19-Jan-15 1:07am    
i checked sql sever configuration manager and tcpport is 1433,
share memory enabled
tcp/ip enabled
named pipes enabled
via disabled

and i had kaspersky virus guard installed on as well. but i disabled the virus guard as well.
still not working................

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[^]
 
Share this answer
 
v2
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[^]
 
Share this answer
 
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.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900