C# の winforms アプリを 2 つ以上のデバイスにインストールしたいのですが、同じデータベースを共有したいと考えています。それを行う方法に関するアドバイスはありますか?
質問する
1099 次
1 に答える
1
あなたの質問は一般的すぎるので、一般的な回答をしようと思います。
おそらく、データベースをデータと共有しているより多くのユーザー向けにアプリを作成しようとしています。おそらく最良の選択は、共有データベースをサーバーに公開し (目的に応じて - lan または wan)、各デバイスからそれに接続することです。
MySQL、MSSQLなど、使用しているデータベースについて言及する必要があります...
その他のことは、オフラインで変更され、時間間隔で同期される各デバイスに 1 つのデータベースが必要な場合ですが、この状況を解決する良い方法ではありません。
あなたがより多くの情報を提供した後、私は更新します。
編集: 最初に必要なこと - パブリック MSSQL サーバーなので、アプリケーションは MSSQL サーバーに ping できるデバイスから起動されます。次に、アプリケーションから接続を作成します。サンプルを提供します。状況に合わせて変更する必要があります (たとえば、クエリごとに 1 つの接続を作成する代わりに、接続プール、トランザクションの使用などを使用する必要があります... )
using System;
using System.Data;
using System.Data.SqlClient;
using System.Text;
public class Program
{
static void Main()
{
string Query = "SELECT * FROM [MyTable]";
//db connection config
DbConfigInfo Config = new DbConfigInfo()
{
ServerAddress = "MyServer",
DbName = "MyDb",
TrustedConnection = true
};
//db adapter for communication
DbAdapter Adapter = new DbAdapter()
{
DbConfig = Config
};
//output with data
DataTable MyDataTable;
if (!Adapter.ExecuteSqlCommandAsTable(Query, out MyDataTable))
{
Console.WriteLine("Error Occured!");
Console.ReadLine();
return;
}
//do actions with your 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 you want create new database in query, set this to master
public string User { get; set; } //user name - only if winauth is off
public string Password { get; set; } //user password - only if winauth is off
public bool TrustedConnection { get; set; } //if integrated windows authenticating under currently logged win user should be used
//creates conn string from 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();
}
}
于 2013-09-04T10:51:15.950 に答える