Click here to Skip to main content
15,885,309 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have problem I don't know how to solve it :(

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
C#
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
Updated 29-Apr-12 10:32am
v3
Comments
[no name] 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 29-Apr-12 15:57pm    
this is the code and the error
Sergey Alexandrovich Kryukov 29-Apr-12 19:29pm    
Exactly.
--SA
[no name] 29-Apr-12 15:58pm    
Well is your SQL server the local host for the second program?
maxpower12345 29-Apr-12 16:50pm    
no its not the localhost
but I use it to try connect it from my computer

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
 
Share this answer
 
v3
Comments
maxpower12345 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 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 30-Apr-12 1:37am    
Espen I doubt if it's WCF related here. Have a look at my answer.
maxpower12345 30-Apr-12 15:23pm    
I will see this
but I don't understand WCF
Espen Harlinn 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.
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 [^]
 
Share this answer
 
Comments
Espen Harlinn 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 30-Apr-12 4:40am    
Thanks.

Yeah localhost, (local), and just a dot (I use dot!) works fine.
maxpower12345 30-Apr-12 15:21pm    
thank it seems helpful :)
I will try this solution
Sandeep Mewara 1-May-12 2:37am    
Ok.
Sergey Alexandrovich Kryukov 30-Apr-12 19:22pm    
Good answer, my 5.
--SA
C#
//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";
 
Share this answer
 
Comments
maxpower12345 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 :)

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