c# - Connect same app on different devices to the same database -


i have c# winforms app , want install on 2 or more devices, want them share same database. advice on how that?

your question general try give general answer.

you're trying create app more users have shared database data. best choice expose shared database on server (by purpose - lan or wan) , connect each device.

you should mention db using, example mysql,mssql,...

other thing if want have 1 db on each device modified off-line , synchronized in time intervals dont thing thats way how solve situation.

i update after provide more info.

edit: first thing have - public mssql server application launched device able ping mssql server. second create connection application, provide sample, should modify best purpose in situation (for example - instead of creating 1 connection each query should use connection pool, usage of transactions, etc...)

using system; using system.data; using system.data.sqlclient; using system.text;  public class program {     static void main()     {         string query = "select * [mytable]";          //db connection config         dbconfiginfo config = new dbconfiginfo()                                   {                                       serveraddress = "myserver",                                       dbname = "mydb",                                       trustedconnection = true                                   };          //db adapter communication         dbadapter adapter = new dbadapter()                                 {                                     dbconfig = config                                 };          //output data         datatable mydatatable;         if (!adapter.executesqlcommandastable(query, out mydatatable))         {             console.writeline("error occured!");             console.readline();             return;         }          //do actions datatable     }    }  public class dbadapter {     //keeps connection info     public dbconfiginfo dbconfig { get; set; }      public bool executesqlcommandastable(string cmdtext, out datatable resulttable)     {         resulttable = null;          try         {             using (sqlconnection conn = new sqlconnection(dbconfig.getconnectionstringformssql2008()))             {                 sqlcommand cmd = new sqlcommand(cmdtext, conn);                 conn.open();                  sqldatareader reader = cmd.executereader();                 datatable returnvalue = new datatable();                 returnvalue.load(reader);                  resulttable = returnvalue;                  return true;             }         }         catch (exception e)         {             return false;         }     } }  public class dbconfiginfo {     public string serveraddress { get; set; } //address of server - ip address or local name     public string dbname { get; set; } //name of database - if want create new database in query, set master     public string user { get; set; } //user name - if winauth off     public string password { get; set; } //user password - if winauth off     public bool trustedconnection { get; set; } //if integrated windows authenticating under logged win user should used       //creates conn string data     public string getconnectionstringformssql2008()     {         stringbuilder constringbuilder = new stringbuilder();          constringbuilder.appendformat("data source={0};", serveraddress)                         .appendformat("initial catalog={0};", dbname);          if (trustedconnection)         {             constringbuilder.append("trusted_connection=true;");         }         else         {             constringbuilder.appendformat("user id={0};", user)                             .appendformat("password={0};", password));         }          return constringbuilder.tostring();     } } 

Comments

Popular posts from this blog

html - How to style widget with post count different than without post count -

How to remove text and logo OR add Overflow on Android ActionBar using AppCompat on API 8? -

javascript - storing input from prompt in array and displaying the array -