categoryservice.cs

来自「一个网上书店的源码」· CS 代码 · 共 42 行

CS
42
字号

using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using MyBookShop.Models;

namespace MyBookShop.DAL
{
	public static partial class CategoryService
	{

		
        public static Category GetCategoryById(int id)
        {
            string sql = "SELECT * FROM Categories WHERE Id = @Id";

            using(SqlDataReader reader = DBHelper.GetReader(sql, new SqlParameter("@Id", id)))
            {
                if (reader.Read())
                {
                    Category category = new Category();

					category.Id = (int)reader["Id"];
					category.Name = (string)reader["Name"];

                    reader.Close();
					
                    return category;
                }
                else
                {
                    reader.Close();
                    return null;
                }
            }
        }
		
	}
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?