2017-12-26 6 views
0

localDBにアクセスしたいです。 SQL Serverの中に私の接続文字列がasp.net mvcプロジェクトのLocalDBにアクセスする方法

<connectionStrings> 
    <add name="DefaultConnection" connectionString="Data Source=(LocalDb)\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\aspnet-Tv3-20171226095229.mdf;Initial Catalog=aspnet-Tv3-20171226095229;Integrated Security=True" 
     providerName="System.Data.SqlClient" /> 
    </connectionStrings> 

以下のようなもので、私のMDFファイルは、この

enter image description here
ようにApp_Dataにしている私は以下のようにデータベースにアクセスするには、次のコードを使用しています:

public static DataSet GetDataSetFromStoredProc(string sql, Dictionary<string, dynamic> dictionary, string _ConnectionString) 
     { 
      try 
      { 
       using (SqlConnection connection2 = new SqlConnection(_ConnectionString)) 
       { 
        SqlCommand cmd = new SqlCommand(sql, connection2); 

        cmd.CommandType = CommandType.StoredProcedure; 
        foreach (KeyValuePair<string, dynamic> pair in dictionary) 
        { 
         cmd.Parameters.AddWithValue(pair.Key, pair.Value); 
        } 
        SqlDataAdapter adp = new SqlDataAdapter(cmd); 
        DataSet ds = new DataSet(); 
        adp.Fill(ds); 
        return ds; 
       } 
      } 
      catch (SqlException err) 
      { 
       // Replace the error with something less specific. 
       // You could also log the error now. 
       throw new ApplicationException("Data error. " + err.Message.ToString()); 
      } 
     } 

public static DataTable GetDataTableFromStoredProc(string sql, Dictionary<string, dynamic> dictionary, string _ConnectionString) 
      { 
       DataSet ds = GetDataSetFromStoredProc(sql, dictionary,_ConnectionString); 

       if (ds.Tables.Count > 0) 
        return ds.Tables[0]; 
       return null; 
      } 

私は以下のような関数を実行しています:

しかし、私は以下のような例外をgetingています:

Data error. A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: SQL Network Interfaces, error: 50 - Local Database Runtime error occurred. Cannot create an automatic instance. See the Windows Application event log for error details.)

これを解決する方法?

+0

は、プロジェクトに追加された.mdfファイルです。 –

+0

@RomanSvitukhaはい –

+0

_これはどこですか? VS/Debugまたはいくつかの "本番環境"。 [SQL Server 2016 Express LocalDBリファレンス](https://docs.microsoft.com/en-us/sql/database-engine/configure-windows/sql-server-2016-express-localdb)、特に_permissions_および_user instance_を参照してください。トピック。 – EdSF

答えて

0

「Initial Catalog = aspnet-Tv3-20171226095229;」を接続文字列から削除してみます。

+0

まだ動作していません.... –

0

Integrated Security = SSPI

+0

まだ動作していません –

関連する問題