调用LINQ to SQL的从code居住在班背后的查询。 [英] Calling Linq To SQL queries residing in classes from code behind.

查看:117
本文介绍了调用LINQ to SQL的从code居住在班背后的查询。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 ASP.NET Web窗体/ C#。我是在一个页面 Customer.aspx .I创建中,我打算使用 Linq到SQL 查询来执行所有数据库 CustomerBLL 相关任务。

I am using ASP.NET Web Forms/C#.I am having a page Customer.aspx.I have created CustomerBLL class in which I plan to use Linq To SQL queries to perform all database related tasks.

考虑这个example.Here是调用方法 GetDateFormat()在我的 CustomerBLL 类从返回的日期格式数据库。这里是code。

Consider this example.Here is a method called GetDateFormat() in my CustomerBLL class which returns Date format from database.Here is the code.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;

namespace CwizBankApp
{
    public class CustomerBLL
    {
        public string GetDateFormat()
        {
            using (var db = new DataClasses1DataContext())
            {
                var format = db.CODEs.Where(code => code.NAME.Equals("dateformat")).SingleOrDefault();  
                return format.DRNAME;   
            }
        }

    }
}

code后面我打电话这样的另一个函数内部此功能。

From code behind I am calling this function inside of another function like this.

public void RetrieveDateFormat()
        {
            //In this function we retrieve the date format
            //from the database to identify if it is british or american


           var format = CustomerBLL.GetDateFormat();  

            //The global variable gDateFormat stores the format
           if (format != null)
           {
               Global.DateFormat = format;
           }

我打电话的功能和第一存储结果,然后检查是否为空或not.Should我做这样或我应该检查它是否为空或不在 CustomerBLL.cs 文件本身?

什么是更好的方法做this.Is我调用函数方式 GetDateFormat()可行的。

What is better way to do this.Is my way of calling the function GetDateFormat() feasible.

也就是维持这种方法的LINQ 在这样的类文件查询,然后叫他们从 code后面认为是一个好的做法呢?

Also is this approach of maintaining Linq queries in such class files and then calling them from code behind considered to be a good practice?

谁能告诉我,如果我在正确的方向走向我还是不?

Can somebody tell me if I am heading in the right direction or not?

任何建议都欢迎。

推荐答案

您调用该函数是确定的方式。然而,你应该在CustomerBLL.cs检查空。

Your way of calling the function is ok. However, you should check for null within CustomerBLL.cs.

这篇关于调用LINQ to SQL的从code居住在班背后的查询。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆