连接到SQL Server数据库没有打开MVC 4

时间:2014-05-23 11:47:30

标签: sql sql-server asp.net-mvc asp.net-mvc-4

我无法连接到我的数据库来运行查询。这里有连接和查询信息:

 string connectionString = "Data Source=(LocalDB)\v11.0;AttachDbFilename=c:\\Webs\\MvcFFL\\MvcFFL\\App_Data\\Players.mdf;Integrated Security=True";

        string queryString =  "Truncate table Players;";
using (SqlConnection connection = new SqlConnection(connectionString))
{
    SqlCommand command = new SqlCommand(queryString, connection);

    connection.Open(); <- Fails here opening the connection

    SqlDataReader reader = command.ExecuteReader();
    try
    {
        while (reader.Read())
        {
            Console.WriteLine(String.Format("{0}, {1}",
                reader[0], reader[1]));
        }
    }
    finally
    {
        // Always call Close when done reading.
        reader.Close();
    }
}

有人可以解释为什么无法打开连接。这是使用SQL Server。我在顶部宣布了这个:

using System.Data.SqlClient;

0 个答案:

没有答案