c# - 如何在asp.net mvc 3.1 c#中按Id获取列行
问题描述
我有一个名为“产品”的数据库表。该表包含具有几个字段的产品,例如“ProductId”、“ProductName”和“ProductPrice”。我想创建一个函数,通过它的 ProductId 获取表中行的值,以便我可以将这些值存储在 cookie 中。如何仅使用 ProductId 作为参数获取一行表“产品”?(没有实体框架)。
解决方案
using (SqlConnection con = new SqlConnection("data source=xxxx;user id=sa;password=xxxxx;integrated security=yes"))
{
SqlDataAdapter da = new SqlDataAdapter("select * from Products where ProductId=" + ProductId);
DataSet ds = new DataSet();
da.Fill(ds);
if (ds.Tables[0].Rows.Count > 0)
{
Console.WriteLine(string.Format("ProductName - {0} and ProductPrice -{1}", ds.Tables[0].Rows[0]["ProductName"].ToString(),
ds.Tables[0].Rows[0]["ProductPrice"].ToString()));
}
}
推荐阅读
- javascript - 在 Ajax 上获取 PDF 的输出流并在另一个文档上打开它
- python - 安装 cx_Freeze
- angular - 如何在角度测试用例中模拟 @viewchild
- sql - SQL 行抛出错误 3134(插入语法)
- vue.js - 如何在 Vue 搜索框中使用查询参数?
- c# - Pag.Previous 页面返回 null
- spring - 是否可以在 Spring MVC 中的 @Scheduled 下调用 @RequestMapping
- paypal - 贝宝快递结帐不与 magento 2.2.4 同步
- node.js - 在 2 个 MongoDB 集合之间引用文档
- java - 使用 Twitter4j,我收集了一堆推文,然后使用 contains() 在其中查找单词,但 contains() 不起作用