C# C getAll函数通知

C# C getAll函数通知,c#,crud,C#,Crud,嗨,我正试图用C语言创建CRUD函数,但我的第一个函数是FetchALL,因为到目前为止,它说并非所有代码路径都返回一个值 这是到目前为止我的密码 public SqlDataReader FetchAll(string tableName) { using (SqlConnection conn = new SqlConnection(_ConnectionString,)) { string query = "SELECT *

嗨,我正试图用C语言创建CRUD函数,但我的第一个函数是FetchALL,因为到目前为止,它说并非所有代码路径都返回一个值

这是到目前为止我的密码

  public SqlDataReader FetchAll(string tableName)
        {  



        using (SqlConnection conn = new SqlConnection(_ConnectionString,))
    { 

    string query = "SELECT * FROM  " + tableName;
    SqlCommand command = new SqlCommand(query, conn);
    using (SqlDataReader reader = command.ExecuteReader())
    conn.Open();


    conn.Close();
           }
        }
    }
}

我可以为您提供更多信息,谢谢。

您需要一个返回语句,该方法才能返回值。

首先,您没有从该方法返回任何内容。我想补充一点,您确定要返回SqlDataReader吗?它是在using块中声明的,因此在您返回它时它将被关闭。我认为您应该重新评估这个函数应该返回什么。

您有一个SqlDataReader的返回类型,但是您没有在代码的任何地方返回任何内容。至少您应该声明数据读取器并按如下方式返回:

public SqlDataReader FetchAll(string tableName)
{
    SqlDataReader reader;

    using (SqlConnection conn = new SqlConnection(_ConnectionString))
    {

        string query = "SELECT * FROM  " + tableName;

        // added using block for your command (thanks for pointing that out Alex K.)
        using (SqlCommand command = new SqlCommand(query, conn))
        {
            conn.Open(); // <-- moved this ABOVE the execute line.
            reader = command.ExecuteReader(); // <-- using the reader declared above.
            //conn.Close(); <-- not needed.  using block handles this for you.
        }
    }

    return reader;
}
以及一个字典,用于将表枚举值映射到将填充到静态构造函数中的表名称:

private static Dictionary<Table, string> _tableNames = new Dictionary<Table, string>(); // populate this in your static constructor.

最后一个注意事项是,我建议您按照约定使用小写骆驼字母命名类级变量,例如_connectionString。

您从不返回任何内容,但您声明了返回SqlDataReader的函数。此外,在string query=SELECT*FROM+tableName;中也可以进行SQL注入;。如果您选择返回SqlDataReader,请确保调用者已正确关闭并处理它,否则很容易开始泄漏sql连接。不要害怕自己尝试解释错误。阅读它,试着逐字理解它。并非所有代码路径,即并非每个执行路径都返回值。这可以通过确保所有路径都返回一个值来满足/解决。问问你自己,是否至少有一条返回语句可以一直到达。从包含一个开始!刘易斯,我已经收回了你最近的修改,因为以这种方式修改你的问题是违反规则的。如果您有新问题,请在网站上提出新问题。我删除了我的答案,因为您的答案更清晰,因为您正在解决一些不同的问题,为了完整性,我在这里会提到几个问题:SQL注入是此函数的首要问题,如果您必须这样做,将表名放入字典中,并使用枚举查找字符串值。此外,参数在使用之前从未检查过。更新了我的注释以提供sql注入问题的解决方案。当然,使用字符串查询=string.FormatSELECT*FROM{0};,查找字典[mytableEnum.Employees];显然,这需要在某处构建公共字典查找字典以及枚举。当使用实体框架时,上下文的作用域可能会结束,如果尚未填充结果,则在尝试访问结果时会出现异常。虽然我不熟悉SqlConnection,但SqlDataReader在阅读时是否需要打开SqlConnection?因为,正如您所说,使用将关闭SqlConnection。如果我在这里错了,请纠正我。@Default Yes-刚刚验证过。谢谢你,我只需要返回一些东西,否则gridview无法获取该功能,但我不确定需要返回什么
private static Dictionary<Table, string> _tableNames = new Dictionary<Table, string>(); // populate this in your static constructor.
public static System.Data.DataSet FetchAll(Table fromTable)
{
    var ret = new System.Data.DataSet();

    using (var conn = new System.Data.SqlClient.SqlConnection(_connectionString))
    {
        string tableName = "";
        if (!_tableNames.TryGetValue(fromTable, out tableName)) throw new ArgumentException(string.Format(@"The table value ""{0}"" is not known.", fromTable.ToString()));
        string query = string.Format("SELECT * FROM {0}", tableName);

        using (var command = new System.Data.SqlClient.SqlCommand(query, conn))
        {
            using (var adapter = new System.Data.SqlClient.SqlDataAdapter(command))
            {
                adapter.Fill(ret);
            }
        }
    }

    return ret;
}