ASP.NET MVC 中调用存储过程和远程表可以使用 ADO.NET 来完成。以下是一个示例代码:
using System.Data;
using System.Data.SqlClient;
public class MyController : Controller
{
public ActionResult CallStoredProcedure()
{
string connectionString = "YourConnectionString";
string storedProcedureName = "YourStoredProcedureName";
using (SqlConnection connection = new SqlConnection(connectionString))
{
using (SqlCommand command = new SqlCommand(storedProcedureName, connection))
{
command.CommandType = CommandType.StoredProcedure;
// 添加存储过程参数
command.Parameters.AddWithValue("@Parameter1", "Value1");
command.Parameters.AddWithValue("@Parameter2", "Value2");
connection.Open();
// 执行存储过程并获取结果
using (SqlDataReader reader = command.ExecuteReader())
{
while (reader.Read())
{
// 处理每一行的数据
}
}
}
}
return View();
}
}
using System.Data;
using System.Data.SqlClient;
public class MyController : Controller
{
public ActionResult CallRemoteTable()
{
string connectionString = "YourConnectionString";
string tableName = "YourRemoteTableName";
using (SqlConnection connection = new SqlConnection(connectionString))
{
string query = string.Format("SELECT * FROM {0}", tableName);
using (SqlCommand command = new SqlCommand(query, connection))
{
connection.Open();
// 执行查询并获取结果
using (SqlDataReader reader = command.ExecuteReader())
{
while (reader.Read())
{
// 处理每一行的数据
}
}
}
}
return View();
}
}
请注意替换示例代码中的 "YourConnectionString"、"YourStoredProcedureName" 和 "YourRemoteTableName" 为实际的连接字符串、存储过程名称和远程表名称。