Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# database Network
I have problem I don't know how to solve it Frown | :(
 
I have 2 program in C#
 
the first is the server program
the second is the client program
 
the first program have database with 2 table
 
I want the client to connect to the database placed in the first program
how can I solve that ?
 

thanks in advance
 
the code is
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Data.Sql;
 
namespace PC
{
    class DataBase
    {
        public static Boolean AddTodo(string name, string email, string password)
        {
            Boolean c = false;
 
            try
            {
                SqlConnection connection = new SqlConnection();
                connection.ConnectionString = @"Data Source=localhost;Initial Catalog=DataBaseWin;Integrated Security=SSPI";
                //Data Source=D:\max\مشروع التخرج\New Code\ServerWin\ServerWin\DataBaseWin.sdf
                connection.Open();
 
                SqlCommand command1 = new SqlCommand("SELECT * FROM UserInfo.  WHERE UserName= @UserName", connection);
                command1.Parameters.Add(new SqlParameter("@UserName", name));
                command1.ExecuteNonQuery();
 
                SqlDataReader reader = command1.ExecuteReader();
                {
                    if (!reader.HasRows)
                    {
                        reader.Close();
 
                        SqlCommand command = new SqlCommand("insert into UserInfo. (UserName, UserEmail, Password) values (@UserName, @UserEmail, @Password)", connection);
                        command.Parameters.Add(new SqlParameter("@UserName", name));
                        command.Parameters.Add(new SqlParameter("@UserEmail", email));
                        command.Parameters.Add(new SqlParameter("@Password", password));
 
                        command.ExecuteNonQuery();
                        c = true;
                    }
                    else
                    {
                        MessageBox.Show("UserName is thaken");
                    }
                }
                connection.Close();
            }
            catch (SqlException e)
            {
                MessageBox.Show(e.Message);
            }
            return c ;
        }
 

        public static Boolean Signin(string name, string password)
        {
            Boolean c = false;
 
            try
            {
 
                SqlConnection connection = new SqlConnection();
                connection.ConnectionString = @"Data Source=localhost;Initial Catalog=DataBaseWin;Integrated Security=SSPI";
                
                connection.Open();
 
                SqlCommand command1 = new SqlCommand("SELECT * FROM UserInfo.  WHERE UserName= @UserName AND Password= @Password ", connection);
                command1.Parameters.Add(new SqlParameter("@UserName", name));
                command1.Parameters.Add(new SqlParameter("@Password", password));
                command1.ExecuteNonQuery();
 
                SqlDataReader reader = command1.ExecuteReader();
                {
                    if (!reader.HasRows)
                    {
                        reader.Close();
                        c = true;
                    }
                    else
                    {
                        MessageBox.Show("Your UserName OR Password Not Right");
                    }
                }
                connection.Close();
            }
            catch (SqlException e)
            {
                MessageBox.Show(e.Message);
            }
            return c;
        }
    }
}
this code in the second program where the database is not there
the database in the server program
 

the error is
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)
Posted 29-Apr-12 9:44am
Edited 29-Apr-12 10:32am
Maciej Los136.3K
v3
Comments
Wes Aday at 29-Apr-12 15:47pm
   
What have you tried? What errors are you getting? Where is the code that demonstrates your problem?
 
Your question makes no sense... I very much doubt that the database is "in" the first program. Improve your question with the actual facts and you might get an answer.
maxpower12345 at 29-Apr-12 15:57pm
   
this is the code and the error
SAKryukov at 29-Apr-12 19:29pm
   
Exactly.
--SA
Wes Aday at 29-Apr-12 15:58pm
   
Well is your SQL server the local host for the second program?
maxpower12345 at 29-Apr-12 16:50pm
   
no its not the localhost
but I use it to try connect it from my computer
Wes Aday at 29-Apr-12 18:30pm
   
Than you need to change your connection string to point to the SQL server computer.
Sandeep Mewara at 30-Apr-12 1:38am
   
It's a configuration issue. See my answer.
SAKryukov at 29-Apr-12 19:29pm
   
What does it mean: "program has database"? This question is not so trivial that you might think.
--SA
Sandeep Mewara at 30-Apr-12 1:37am
   
It is a common error that users face while connection SQL DB. See my answer.
SAKryukov at 30-Apr-12 19:23pm
   
Probably you are right. I'm looking at your answer, that is a good one.
--SA
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

//Provide the IP address of the database server in you Data Source instead of 'localhost'  like below
connection.ConnectionString = @"Data Source='192.168.1.127';Initial Catalog=DataBaseWin;Integrated Security=SSPI";
  Permalink  
Comments
maxpower12345 at 30-Apr-12 15:00pm
   
thanks ..... but
I try this one but its dose not work
because I don't know the IP for server the database placed in :)
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

Generally, it is surface area configuration error or sql service configuration issue. Have a look at this thread for potential reasons and resolutions: Resolving A network-related or instance-specific error occurred while establishing a connection to SQL Server[^]
 
If needed, See steps for setting up Surface area configuration here..

Here, look at the following blog entries related to the same:
Under the default settings SQL Server does not allow remote connections[^]
It says:
I. Make sure that Remote Connections are enabled in Surface Area Configuration tool
II. Check Whether TCP/IP protocol is enabled in the SQL Server Configuration Manager
III. Make sure that SQL server Browser service is running
 
Also look at this:
SQL SERVER – FIX : ERROR : (provider: Named Pipes Provider, error: 40...[^]
It says:
I. SQL Server should be up and running.
II. Enable TCP/IP in SQL Server Configuration
III. Open Port in Windows Firewall
IV. Enable Remote Connection
V. Enable SQL Server Browser Service
VI. Create exception of sqlbrowser.exe in Firewall
VII. Recreate Alias
 

If these does not resolve your issue, have a look at these thread:
How to configure SQL Server 2005 to allow remote connections[^]
Similar issues: A network-related..[^]
SQL SERVER FIX ERROR (provider: Named Pipes [^]
  Permalink  
Comments
Espen Harlinn at 30-Apr-12 3:46am
   
5'ed! Absolutely relevant - the system has to be configured to use tcp/ip.
For access to the a local database I usually use shared memory.
 
I had a look at his connection string and I've never seen localhost before, I use (local) as this appearantly allows me to work with both tcp/ip and shared memory.
Sandeep Mewara at 30-Apr-12 4:40am
   
Thanks.
 
Yeah localhost, (local), and just a dot (I use dot!) works fine.
maxpower12345 at 30-Apr-12 15:21pm
   
thank it seems helpful :)
I will try this solution
Sandeep Mewara at 1-May-12 2:37am
   
Ok.
SAKryukov at 30-Apr-12 19:22pm
   
Good answer, my 5.
--SA
Sandeep Mewara at 1-May-12 2:38am
   
Thanks.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

One place to start is here: WCF Data Services[^]
 
Then there is Introduction to ADO.NET Data Services/RIA Services[^] By Sacha Barber
 
[Update]
Try replacing localhost with (local)
 
Data Source=(local)
 
Best regards
Espen Harlinn
  Permalink  
v3
Comments
maxpower12345 at 29-Apr-12 16:49pm
   
thanks
I try to download WCF
but always dose not work
I need the source "web site" for WCF to download it
Espen Harlinn at 29-Apr-12 16:54pm
   
The downloads on Microsofts site are for pre 4.0 versions of .Net, so chances are you don't need to download any core libraries.
Sandeep Mewara at 30-Apr-12 1:37am
   
Espen I doubt if it's WCF related here. Have a look at my answer.
maxpower12345 at 30-Apr-12 15:23pm
   
I will see this
but I don't understand WCF
Espen Harlinn at 30-Apr-12 16:21pm
   
This "connect to database placed in another program" makes me think of a threetiered solution, have a look at: http://en.wikipedia.org/wiki/Multitier_architecture
 
WCF enables fairly easy development of multitiered solutions.
SAKryukov at 30-Apr-12 19:23pm
   
Good and relevant, a 5.
--SA

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

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 901
1 OriginalGriff 445
2 CPallini 255
3 Richard Deeming 175
4 George Jonsson 161
0 OriginalGriff 5,540
1 Sergey Alexandrovich Kryukov 4,668
2 CPallini 4,540
3 George Jonsson 3,102
4 Gihan Liyanage 2,445


Advertise | Privacy | Mobile
Web03 | 2.8.140916.1 | Last Updated 30 Apr 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100