-2
connection a=new connection("Data Source= server name initial catalog = database name user Id=user1 password=user1");

I am confused that when I deploy the .net desktop application on client side server name, username and password will be different any solution for that please tell me I am new for .net

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
user2353848
  • 157
  • 3
  • 9

4 Answers4

0

Yes, keep the connection string in the settings file. And update it when you deploy. You can read the connection string at runtime.

Ehsan
  • 31,833
  • 6
  • 56
  • 65
0

1) Check your connection string, missing semi-colon.

 String connectionString ="Data Source=(local);Initial Catalog=AdventureWorks;"
        + "User ID=sa;Password=xxx;Integrated Security=false;";
    SqlConnection con = new SqlConnection(connectionString );

2) Usually I used to keep database configuration in App.config and access it like

App.config:

<?xml version="1.0"?>
<configuration>
  <connectionStrings>
    <add name="CharityManagement" 
    connectionString="Data Source=XXXXXXXX;Initial Catalog=XXXXXXXXX;User ID=XXX;Password=XXXXX;Integrated Security=false;"/>
  </connectionStrings>
</configuration>

C# Code:

var connectionString=ConfigurationManager.ConnectionStrings["CharityManagement"].ConnectionString;
    SqlConnection connection = new SqlConnection(connectionString.ToString());

So client can change the database configuration in App.config and it is handled in runtime.

Updates:

What kind of change?

  1. It depends upon the client database.
  2. Client may have different Datasource and Authentication.
  3. If SQL Authentication = > client has to give User ID and Password with Integrated Security=false Else If Window Authentication => no need of username and password with Integrated Security=true
Praveen
  • 55,303
  • 33
  • 133
  • 164
  • I am using sql authentication then what about the username and password – user2353848 Sep 05 '13 at 05:04
  • what kind of changes we have to make in connection string on client side after installation the application – user2353848 Sep 05 '13 at 05:06
  • @user2353848 I have add in my answer. If you're giving the username and password explicitly then `Integrated Security=false;` incase if it is a windows authentication then `Integrated Security=true;` without username and password. – Praveen Sep 05 '13 at 05:07
  • will you please tail me the changes that that have to make at client side after installation – user2353848 Sep 05 '13 at 05:25
  • @user2353848 I have updated in my answer. Have a look at it. – Praveen Sep 05 '13 at 05:25
  • is app.config file not a part of exe. if it is a part of exe then how can i make changes in app.config file – user2353848 Sep 05 '13 at 05:30
  • @user2353848 After building your application, for example: you application name: Myapp.exe then `app.config` will look like `Myapp.exe.config` It seems you need a lot of practice. I will suggest to do some trail and errors. Try different attempts, you will understand. – Praveen Sep 05 '13 at 05:42
  • @user2353848 If you get time, have a look at http://stackoverflow.com/questions/1431742/what-is-app-config-for and http://msdn.microsoft.com/en-us/library/vstudio/ms184658.aspx – Praveen Sep 05 '13 at 05:44
0

Make sure you are using an ip address or correct domain name for the server because your client can not connect to the server if its not part of your local network if you use the server name or local host.

Marko
  • 2,734
  • 24
  • 24
0

1.its very simple as people said earlier keep your connection string in App.config file and you can easily access that connection string in your code . SqlConnection conn = new SqlConnection(); conn.ConnectionString = System.Configuration.ConfigurationSettings.AppSettings["DBConnectionString"]; "DBConnectionString" will be your part of App.config in section 2. Now when you deploy your application to client machine just change the connection string in App.config file according to clients environment.