如何在asp.net mvc项目中访问LocalDB

时间:2021-04-08 10:26:06

I want to access localDB. My connection string in sql server is like below

我想访问localDB。我在sql server中的连接字符串如下所示

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

And my mdf file is in app_data like this

我的mdf文件就像这样在app_data中

如何在asp.net mvc项目中访问LocalDB
i am using following code to access the database like below:

我正在使用以下代码访问数据库,如下所示:

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

Now i am executing the function like below:

现在我正在执行如下功能:

GetDataTableFromStoredProc("down_data", dictionary, System.Configuration.ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString)

but i am geting an exception like below:

但我正在寻找如下例外情况:

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. )

数据错误。建立与SQL Server的连接时发生与网络相关或特定于实例的错误。服务器未找到或无法访问。验证实例名称是否正确,以及SQL Server是否配置为允许远程连接。 (提供程序:SQL网络接口,错误:50 - 发生本地数据库运行时错误。无法创建自动实例。有关错误详细信息,请参阅Windows应用程序事件日志。)

How to solve this??

怎么解决这个?

2 个解决方案

#1


0  

Try removing "Initial Catalog=aspnet-Tv3-20171226095229; " from the connection string.

尝试从连接字符串中删除“Initial Catalog = aspnet-Tv3-20171226095229;”。

#2


0  

Try to user Integrated Security=SSPI

尝试使用Integrated Security = SSPI

#1


0  

Try removing "Initial Catalog=aspnet-Tv3-20171226095229; " from the connection string.

尝试从连接字符串中删除“Initial Catalog = aspnet-Tv3-20171226095229;”。

#2


0  

Try to user Integrated Security=SSPI

尝试使用Integrated Security = SSPI