【问题标题】:Connect same app on different devices to the same database [closed]将不同设备上的同一个应用程序连接到同一个数据库[关闭]
【发布时间】:2013-09-04 09:56:55
【问题描述】:

我有一个 C# winforms 应用程序,我想将它安装在两个或更多设备上, 但我希望他们共享同一个数据库。 关于如何做到这一点的任何建议?

【问题讨论】:

  • 很遗憾,但你有什么问题?你试过什么?
  • 我有一个使用数据库的应用程序,我想在多个设备上运行它,但我希望任何设备上的应用程序共享一个数据库,以便当任何设备上的任何人更改db 其他设备上的其他应用程序看到 db 更改。我不知道该怎么做
  • 好的,那你有什么尝试?
  • 我不知道该尝试什么
  • 你是如何连接到数据库的?您使用什么方法来检索数据?你用的是什么数据库?你的数据库是如何设置的?您的数据库可以通过网络访问吗?你的连接字符串是什么?...更多细节!

标签: c# winforms ado.net ado


【解决方案1】:

你的问题太笼统了,所以我试着给你一个笼统的答案。

您可能正在尝试为更多与数据共享数据库的用户创建应用程序。可能最好的选择是在服务器上公开您的共享数据库(按目的 - lan 或 wan)并从每个设备连接到它。

您应该提及您正在使用的数据库,例如 MySQL、MSSQL、...

另一件事是,如果您想在每台设备上拥有一个离线修改并按时间间隔同步的数据库,但我不认为这是解决这种情况的好方法。

你提供更多信息后我会更新。

编辑: 您必须做的第一件事 - 公共 MSSQL 服务器,因此应用程序将从能够 ping MSSQL 服务器的设备启动。 其次,您从应用程序创建到它的连接,我为您提供示例,您应该根据您的情况修改它以达到最佳目的(例如 - 而不是为每个查询创建一个连接,您应该使用连接池、事务的使用等...... )

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();
    }
}

【讨论】:

  • 我使用 sql server 2008
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-09-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-09-03
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多