带有sql server的C#wpf应用程序仅适用于我的电脑

时间:2018-05-31 13:48:01

标签: c# sql-server entity-framework

我有一个带有sql server express的wpf应用程序,它在我的电脑上完美运行,但在另一台计算机上它不起作用,我使用的是Entity Framework,我做了一个方法来获取sql server的服务器名称和实例的名称和创建数据库,我还创建了一个方法来创建表,如果它不存在,我使用登录和默认密码插入用户访问sisitema。

我的连接字符串

  <connectionStrings>
<add name="BarberSystem.Properties.Settings.BancoTesteConnectionString" connectionString="Data Source=.\SQLEXPRESS;Initial Catalog=BARBER_DATABASE;Integrated Security=True" providerName="System.Data.SqlClient" />
<add name="BARBER_DATABASE" connectionString="data source=.\SQLEXPRESS;initial catalog=BARBER_DATABASE;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework" providerName="System.Data.SqlClient" />

我的方法

    public string ConnectionString { get; protected set; }
    public SqlServer(string connectionString) {
        ConnectionString = connectionString;
    }

    public static void createIfNotExists(string connectionString) {
        new SqlServer(connectionString).createIfNotExists();
    }

    public void createIfNotExists() {
        try {
            var connectionStringBuilder = new SqlConnectionStringBuilder(ConnectionString);
            var databaseName = connectionStringBuilder.InitialCatalog;
            connectionStringBuilder.InitialCatalog = "master";
            using (var connection = new SqlConnection(connectionStringBuilder.ToString())) {
                connection.Open();
                using (var cmd = connection.CreateCommand()) {
                    cmd.CommandText = string.Format("select * from master.dbo.sysdatabases where name='{0}'", databaseName);
                    using (var reader = cmd.ExecuteReader()) {
                        if (reader.HasRows) {
                            return;
                        }
                        reader.Close();
                        cmd.CommandText = string.Format("CREATE DATABASE {0}", databaseName);
                        cmd.ExecuteNonQuery();
                    }
                }
            }
        }
        catch (Exception e) {
            Log.logException(e);
            Log.logMessage(e.Message);
        }

    }

 public static string getServer(){
        string servidor = string.Empty;
        System.Data.Sql.SqlDataSourceEnumerator instancia = System.Data.Sql.SqlDataSourceEnumerator.Instance;
        System.Data.DataTable dados = instancia.GetDataSources();
        foreach (DataRow item in dados.Rows) {
            servidor = item["ServerName"].ToString();
        }
        return servidor;
    }

   public static string getInstance() {
        string instancia = string.Empty;
        System.Data.Sql.SqlDataSourceEnumerator instance = System.Data.Sql.SqlDataSourceEnumerator.Instance;
        System.Data.DataTable dados = instance.GetDataSources();
        foreach (DataRow item in dados.Rows) {
            instancia = item["InstanceName"].ToString();
        }
        return instancia;
    }

1 个答案:

答案 0 :(得分:0)

检查您的防火墙,您必须允许连接到C:\ Program Files \ Microsoft SQL Server \ MSSQL12.SQLEXPRESS2014 \ MSSQL \ Binn \ sqlservr.exe