我想在 asp.net mvc 3.1(无 ORM)中显示 sql table 中的特定行
I want to show a specific row from sql table in asp.net mvc 3.1 (without ORM)
我有一个名为 products 的 table,它有 9 行产品,它们都有自己独特的 productId。我想展示 productId = 3 的产品。我该怎么做,或者你知道哪个 van 向我解释的教程吗?
MVC - 模型视图控制器,实际上 MVC 不需要任何数据库。假设您有对象模型、模型视图和控制器 执行您需要的任何操作。因此将对象模型持久化到数据库中不需要使用 ORM。您可以调用 DB 存储过程,Direct table,SQL Plan Text 来持久化数据。
来自 here 的示例,用于执行存储过程
static void GetSalesByCategory(string connectionString,
string categoryName)
{
using (SqlConnection connection = new SqlConnection(connectionString))
{
// Create the command and set its properties.
SqlCommand command = new SqlCommand();
command.Connection = connection;
command.CommandText = "SalesByCategory";
command.CommandType = CommandType.StoredProcedure;
// Add the input parameter and set its properties.
SqlParameter parameter = new SqlParameter();
parameter.ParameterName = "@CategoryName";
parameter.SqlDbType = SqlDbType.NVarChar;
parameter.Direction = ParameterDirection.Input;
parameter.Value = categoryName;
// Add the parameter to the Parameters collection.
command.Parameters.Add(parameter);
// Open the connection and execute the reader.
connection.Open();
using (SqlDataReader reader = command.ExecuteReader())
{
if (reader.HasRows)
{
while (reader.Read())
{
Console.WriteLine("{0}: {1:C}", reader[0], reader[1]);
}
}
else
{
Console.WriteLine("No rows found.");
}
reader.Close();
}
}
}
命令类型枚举
StoredProcedure
TableDirect
Text
我有一个名为 products 的 table,它有 9 行产品,它们都有自己独特的 productId。我想展示 productId = 3 的产品。我该怎么做,或者你知道哪个 van 向我解释的教程吗?
MVC - 模型视图控制器,实际上 MVC 不需要任何数据库。假设您有对象模型、模型视图和控制器 执行您需要的任何操作。因此将对象模型持久化到数据库中不需要使用 ORM。您可以调用 DB 存储过程,Direct table,SQL Plan Text 来持久化数据。
来自 here 的示例,用于执行存储过程
static void GetSalesByCategory(string connectionString,
string categoryName)
{
using (SqlConnection connection = new SqlConnection(connectionString))
{
// Create the command and set its properties.
SqlCommand command = new SqlCommand();
command.Connection = connection;
command.CommandText = "SalesByCategory";
command.CommandType = CommandType.StoredProcedure;
// Add the input parameter and set its properties.
SqlParameter parameter = new SqlParameter();
parameter.ParameterName = "@CategoryName";
parameter.SqlDbType = SqlDbType.NVarChar;
parameter.Direction = ParameterDirection.Input;
parameter.Value = categoryName;
// Add the parameter to the Parameters collection.
command.Parameters.Add(parameter);
// Open the connection and execute the reader.
connection.Open();
using (SqlDataReader reader = command.ExecuteReader())
{
if (reader.HasRows)
{
while (reader.Read())
{
Console.WriteLine("{0}: {1:C}", reader[0], reader[1]);
}
}
else
{
Console.WriteLine("No rows found.");
}
reader.Close();
}
}
}
命令类型枚举
StoredProcedure
TableDirect
Text