简体   繁体   中英

C # wpf application with sql server works only on my pc

I have a wpf application with sql server express, it works perfectly on my pc, but on another computer it does not work, I am using Entity Framework, I made a method to get the server name of the sql server and the name of the instance and create the database , I also made a method to create the tables if it does not exist and I make an insert with login and default password for the user to access the sisitema.

My connection string

  <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" />

my methods

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM