Click here to Skip to main content
15,890,512 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
HI :confused:

I have a win app program that I wrote it in C#.
My problem Is connecting to Sql server2005 from Other clients.

I wrote Another simple program for testing my connection. I used try-Catch for opening connections But it can not Open connection.

I have been searching a lot of pages and I couldn't solve my problem.

I used Two Button and it is my project:

http://www.4shared.com/file/HcKYuam_/IP_online.html[^]

Also I Have Database (in the name of Test) with one Table(in the name of Table_1).

Is it something wrong? Or I have to adjust something.

please Help me

thanks
Posted
Updated 18-Oct-10 21:22pm
v3
Comments
Dalek Dave 19-Oct-10 3:22am    
Minor Edit for Readability.

You need to allow remote connections for your MS SQL server, so that other clients could access it. Refer this link for steps - http://support.microsoft.com/kb/914277[^]

An important point to note - I guess you will distribute this application to others. So make sure you are not having the connection string in plain text. Encrypt the connection string/ the password when you distribute the app.
 
Share this answer
 
Comments
Dalek Dave 19-Oct-10 3:22am    
Good Answer.
SqlConnection con=new SqlConnection ("Server=servername;database=databasename;integrated security=true");

SqlCommand cmd=new SqlCommand ("your insert query",con);
con.Open();
cmd.ExecuteNonQuery();
con.Close();
 
Share this answer
 
Comments
Karthik. A 13-Oct-10 17:57pm    
@poster - Note that if you are setting integrated security=true, it will only work in the machine that houses the ms sql server (w/ windows authentication enabled)
In your Code I can clearly see that you are probably not using ConnectionString in which format it should be.

Please have a look at THIS[^] to have more clarity about ConnectionString to DB.

Please vote and Accept Answer if it Helped.
 
Share this answer
 
Comments
dearabi 13-Oct-10 10:05am    
Hi Dear

Thank you For Your Guidelines.I saw your Page and read it Carefully. I used Connection Via IP from That Page(Data Source=190.190.200.100,1433;Network Library=DBMSSOCN;Initial Catalog=myDataBase;User ID=myUsername;Password=myPassword; ) As you will see in button#3 in my project.
First of All I got Error1(I attached Errors)and The Compiler passed from Catch and I got error3.When I added (Trusted_Connection=Yes)My problem solved but couldn't connect to my DB from Other clients.I used The Idea of mohd Wasif But it my promleb reminded yet.

http://www.4shared.com/file/0e9l8mcj/IPErrors.html

Now,What Must I do? Please Help Me ?
Hi Dears :(( :(( :((

Thank you For Your Guidelines.I saw your Page and read it Carefully. I used Connection Via IP from That Page(

Data Source=xxx.xxx.xx.xx,1433;Network Library=DBMSSOCN;Initial Catalog=myDataBase;User ID=myUsername;Password=myPassword; )


As you will see in button#3 in my project.
First of All I got Error1(I attached Errors)and The Compiler passed from Catch and I got error3.When I added (Trusted_Connection=Yes)My problem solved but couldn't connect to my DB from Other clients.I used The Idea of mohd Wasif But my problem is reminded yet.

http://www.4shared.com/file/0e9l8mcj/IPErrors.html

Now,What Must I do? Please Help Me ?
 
Share this answer
 
Hi Karthik

Thank you for your suggestion. It was great.By your Idea,I could Solve some parts of my problem.
I login into Clients as a administrator and Easily by below connection I can Insert data In Db:

SqlConnection oSQLConn = new SqlConnection();

oSQLConn.ConnectionString = "Network Library=DBMSSOCN;" +
"Data Source=MyServerName;" +
"Initial Catalog=test;" +
"Trusted_Connection=yes";
But When Others login, They Can not insert Data.I add them as admin(in that clients) in those clients But my problem remind yet.

Do you Have Any Idea ?

And I can not understand These Sentences
{
An important point to note - I guess you will distribute this application to others. So make sure you are not having the connection string in plain text. :omg: Encrypt the connection string/ the password :confused:when you distribute the app.
}

Please Help me?especially highlight texts

Thank you
 
Share this answer
 
Comments
Karthik. A 19-Oct-10 9:51am    
By saying "I add them as admin(in that clients) in those clients" do you mean you have a user account in MS SQL that is used in your connection string? If not create a user that others could use. I would advice you against using the "sa" user in your connection string. Once you create a user that others could use to access you SQL server, make sure the user has datawriter permissions for that database. You could also make that user the owner of the database, but thats risky. Follow these steps to give the user datawriter access for a database

Expand Security -> Logins -> right click on the user you created, choose "Properties". Then select "User Mapping". Choose the database they are going to access and give "db_datawriter" database role. Now they should be able to write to the database too. Be careful as to what they write to the database!
Karthik. A 19-Oct-10 9:53am    
As for the part you didn't understand, refer to this link - http://www.dotnetprofessional.com/blog/post/2008/03/03/Encrypt-sections-of-WebConfig-or-AppConfig.aspx

This is assuming your connection strings are in app.config file. If you unaware about this, read about that too.
dearabi 20-Oct-10 10:19am    
Hi Dear
Thanks For Your Great Guide.I can not solve my problem again . Look at these pics and tell me your Idea.

http://www.4shared.com/file/Dbtt9xF5/users.html
HI :)

Thanks to all my friends that help me for solving my problem. I finally solved my problem .After adding the Users in sql(my DB) and using firwall.cpl for its exceptions (as my friend Karthik told mein this Page

http://support.microsoft.com/kb/914277[^]

)
,I used this Connection and it works.

I will copy my connection here maybe another person use it .


"Data Source=MyserverName;Initial Catalog=MyDataBase;Persist Security

Info=True;User ID=sa;password=password of Sa";

Thanks :-O
 
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