ASP.NET中的SQL查询

ASP.NET中的SQL查询,asp.net,entity-framework,Asp.net,Entity Framework,我对ASP.NET很陌生。我有Visual Studio Express 2013和MSSql服务器,其中包含一组数据库。我正在观看一些关于MVC/Entity framework的教程,以从表中检索并显示数据。但是,它使用的是我不熟悉的LINQtoSQL。我只需要编写一个sql查询,将信息合并到两个表中,并在视图中显示。我在网上找不到这个简单的教程。谁能给我一个提示吗?要运行一个简单的查询,您需要创建到数据库的连接,如下所示: var connection = new SqlConnectio

我对ASP.NET很陌生。我有Visual Studio Express 2013和MSSql服务器,其中包含一组数据库。我正在观看一些关于MVC/Entity framework的教程,以从表中检索并显示数据。但是,它使用的是我不熟悉的LINQtoSQL。我只需要编写一个sql查询,将信息合并到两个表中,并在视图中显示。我在网上找不到这个简单的教程。谁能给我一个提示吗?

要运行一个简单的查询,您需要创建到数据库的连接,如下所示:

var connection = new SqlConnection("your connecting string");
using (connection)
    {
        SqlCommand command = new SqlCommand(
          "SELECT CategoryID, CategoryName FROM Categories;",
          connection);
        connection.Open();

       //fetches the data by executing the command
        SqlDataReader reader = command.ExecuteReader();

        if (reader.HasRows)
        {
            while (reader.Read())
            {
                //You can process data here , read it from reader and process it
                // the index 0 and 1 indicate your query columns
                var somedata = reader.GetInt32(0) + reader.GetString(1));
            }
        }
        else
        {
            //no data returned from query
        }
        reader.Close();
    }
一旦有了这个命令,您就可以连接到数据库并使用名为SqlCommand的命令进行查询,如下所示:

var connection = new SqlConnection("your connecting string");
using (connection)
    {
        SqlCommand command = new SqlCommand(
          "SELECT CategoryID, CategoryName FROM Categories;",
          connection);
        connection.Open();

       //fetches the data by executing the command
        SqlDataReader reader = command.ExecuteReader();

        if (reader.HasRows)
        {
            while (reader.Read())
            {
                //You can process data here , read it from reader and process it
                // the index 0 and 1 indicate your query columns
                var somedata = reader.GetInt32(0) + reader.GetString(1));
            }
        }
        else
        {
            //no data returned from query
        }
        reader.Close();
    }

你为什么要问关于LINQ到SQL的问题。你可能会感到困惑。不要用它。使用实体框架。