Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/268.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在C中编码时,SQL字符串不返回任何值_C#_Sql Server_Visual Studio - Fatal编程技术网

C# 在C中编码时,SQL字符串不返回任何值

C# 在C中编码时,SQL字符串不返回任何值,c#,sql-server,visual-studio,C#,Sql Server,Visual Studio,使用visual studio 2012 express创建web api,该api连接到sql数据库并搜索术语并返回值。 当我运行代码时,sql字符串起作用。我可以复制字符串并将其加载到SQL server 2014中的查询中,然后返回预期结果。 但是,当我在这里运行代码时,返回数据上的sql字符串的所有值都是空的 有人有什么建议吗 多谢各位 using System; using System.Collections.Generic; using System.Data; using Sys

使用visual studio 2012 express创建web api,该api连接到sql数据库并搜索术语并返回值。 当我运行代码时,sql字符串起作用。我可以复制字符串并将其加载到SQL server 2014中的查询中,然后返回预期结果。 但是,当我在这里运行代码时,返回数据上的sql字符串的所有值都是空的

有人有什么建议吗

多谢各位

using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Threading.Tasks;

namespace WebAPi
{
public class TableBusinessTerm : TableGenericBase<stBusinessTerm>
{
    private string TableName = "[MetadataRepository].[dbo].[QrySocialGraphMobile]";
    public override void CreateTable()
    {
        throw new NotImplementedException();
    }

    public stBusinessTerm GetBusinessTerm(string termName)
    {
        string sql = "Select * From " + TableName + " Where BusinessTerm = '" + termName + "'";
        var data = new stBusinessTerm();
        bool result = GetRecord(ref data, sql);
        return data;
    }
    protected override void DatabaseRow_Get(DataRow dr, ref stBusinessTerm data)
    {
        DatabaseUtilities.DataRow_Get(dr, "BusinessTerm", ref data.BusinessTerm);
        DatabaseUtilities.DataRow_Get(dr, "BusinessTermLongDesc", ref data.BusinessTermLongDesc);
        DatabaseUtilities.DataRow_Get(dr, "DomainCatID", ref data.DomainCatID);
        DatabaseUtilities.DataRow_Get(dr, "SystemName", ref data.SystemName);
        DatabaseUtilities.DataRow_Get(dr, "DataSteward", ref data.DataSteward);
        DatabaseUtilities.DataRow_Get(dr, "DomainCatName", ref data.DomainCatName);
        DatabaseUtilities.DataRow_Get(dr, "GoldenSource", ref data.GoldenSource);
        DatabaseUtilities.DataRow_Get(dr, "GTS_table", ref data.GTS_table);
        DatabaseUtilities.DataRow_Get(dr, "TableOwnerName", ref data.TableOwnerName);
        DatabaseUtilities.DataRow_Get(dr, "Synonym", ref data.Synonym);
    }

    protected override void DatabaseRow_Get(long autoIncrementedID, ref stBusinessTerm data)
    {
        throw new NotImplementedException();
    }

    protected override void DatabaseRow_Set(ref DataRow dr, stBusinessTerm data)
    {
        throw new NotImplementedException();
    }
}

}您当前的数据代码是。。。很复杂,很难理解,而且由于缺少很多信息,很难直接评论正在发生的事情。然而我强烈怀疑你在不必要地让自己的生活变得非常艰难。看起来您有一个stBusinessTerm类型,我猜是一个结构,虽然这几乎肯定是一个错误的选择,但这是一个单独的问题-而且:您通常应该避免使用类似于st的前缀,而公共字段是另一个错误的选择,几乎可以肯定的是,它与列名完全匹配。在这种情况下,我强烈建议使用工具,例如。以下是正确参数化的完整代码:

public stBusinessTerm GetBusinessTerm(string termName)
{
    using(var conn = GetConnectionFromSomewhere())
    {
        return conn.QuerySingleOrDefault<stBusinessTerm>(@"
Select * From [MetadataRepository].[dbo].[QrySocialGraphMobile]
Where BusinessTerm = @termName",
            new { termName }); // parameters
    }
}

而且。。。就这样!基本上,Dapper的工作原理是,人们为执行命令和使用结果而编写的大多数代码都是死记硬背、枯燥乏味的,而且通常效率低下且有问题——如果一个库为您做了所有这些事情会更好。

为什么要使用ref???因为我不能完全确定我在真实地做什么,这是我避免s sytnax错误的唯一方法。假设您引用的是行bool result=GetRecordref data,sql;首先返回数据:永远不要连接输入termName来创建SQL-这真的很危险-您应该查看参数。然而,这里更相关的是,您似乎正在使用一些您没有向我们展示的自定义代码。如果看不到GetRecord做了什么或DatabaseRow\u Get做了什么,我们就无法判断问题出在哪里。现在,它只会抛出NotImplementedException。什么是商业术语?GetRecord和DatabaseRow\u Get之间的关系是什么?因为现在:没有什么可以调用它,GetRecord也不存在不必要的复杂性。为什么不使用sqldataadapter?好的,也许需要更多的细节。正在从网页传递术语名称。然后创建字符串sql,这就是我连接输入的原因。我从未使用过sqlDataAdapter,所以我认为创建sql字符串然后将值传递给数据行会更容易——显然不是这样。我希望这是一个愚蠢的东西,比如我留下了一个逗号,或者别的什么,但是现在看到它比那个更复杂了。谢谢你到目前为止的建议