簡體   English   中英

使用WCF服務中的參數選擇數據庫中的表

[英]select table in database using a parameter in WCF service

我仍然是編寫C#和SQL的初學者,並且想知道是否有人可以幫助我解決這個基本問題。 我在互聯網上四處張望,但仍然陷入困境。

我正在嘗試編寫WCF服務來訪問我的數據庫。 我只需要一種方法:

public PathDto GetPath(string src, string trg)
    {
        using (var context = new PathsEntities())
        {
            var p = (
                    from a
                    in context.src
                    where a.Target = trg
                    select a).Distance, Path;
        }
    }

其中參數src是表名,字符串trg是實體的主鍵。

Visual Studio給我錯誤:... pathsService does not contain a definition for src因為它試圖查找表“ src”,而不是查找變量中包含的字符串。

如何在查詢語句中使用參數?

我將假設您正在使用DbContext EF5.0東西

public PathDto GetPath(string tableType, string id)
{
    using (var context = new PathsEntities())
    {
            var type = Type.GetType(tableType);
            var p = context.Set(type).Find(id);
            return (PathDto)p;
    }
}

似乎您不使用EF 5.0,而只有EF 4.0,並且正在使用ObjectContext。 嘗試一下...不知道它是否有效,因為我並不真正使用EF 4.0。 或者下載EF 5.0

public PathDto GetPath(string tableType, string id)
{
    using (var context = new PathsEntities())
    {
            var type = Type.GetType(tableType);
            var p = context.GetObjectByKey(new EntityKey(tableType, "id", id));
            return (PathDto)p;
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM