Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/339.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减少从数据库获取数据的方法数量_C#_Methods_Code Reuse - Fatal编程技术网

C# c减少从数据库获取数据的方法数量

C# c减少从数据库获取数据的方法数量,c#,methods,code-reuse,C#,Methods,Code Reuse,我有一个处理DB列元数据的类。该类的属性之一是有问题的表。这是通过构造函数传递给对象的。同样在构造函数中,我应用了一些逻辑来分配类中的其他变量。要做到这一点,有许多私有方法可以连接到数据库,查询有关表的信息,并向变量返回值 我的问题是,我有很多不同的方法做几乎相同的事情,但返回不同的数据类型。例如,我的代码是这样的 public Column(string tableName) { strTableName = tableName; pkColumnName = GetPKColum

我有一个处理DB列元数据的类。该类的属性之一是有问题的表。这是通过构造函数传递给对象的。同样在构造函数中,我应用了一些逻辑来分配类中的其他变量。要做到这一点,有许多私有方法可以连接到数据库,查询有关表的信息,并向变量返回值

我的问题是,我有很多不同的方法做几乎相同的事情,但返回不同的数据类型。例如,我的代码是这样的

public Column(string tableName)
{
   strTableName = tableName;
   pkColumnName = GetPKColumnName(tableName);
   pkColumnLenght = GetPKColumnLenght(tableName);
}

private string GetPKColumnName(string tableName)
{
   string query = String.Format("SELECT myColName FROM myTable where myTableName = {0}",  tableName);
   string result = "";
   try
   {
       using(SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["MyDB"].ConnectionString))
         {
          con.Open();
          using (SqlCommand command = new SqlCommand(query, con))
          {
             result = (string)command.ExecuteScalar();
          }
         }
    }
    catch (SqlException ex)
    {
         Console.WriteLine(ex.Message);
    }
 return result;
}

private int GetPKColumnLenght(string tableName)
    {
       string query = String.Format("SELECT myColLenght FROM myTable where myTableName = {0}",  tableName);
       int result = 0;
       try
       {
           using(SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["MyDB"].ConnectionString))
             {
              con.Open();
              using (SqlCommand command = new SqlCommand(query, con))
              {
                 result = (int)command.ExecuteScalar();
              }
             }
        }
        catch (SqlException ex)
        {
             Console.WriteLine(ex.Message);
        }
     return result;
    }
还有很多其他类似的方法。这对我来说不是很好,所以我想知道这样的事情的最佳实践是什么


我是否应该将返回类型声明为一个对象,并在将返回值赋给变量时进行数据类型转换?

我的答案和另一个假设不同的问题。在我看来,您试图从特定列中查询单个值,并且必须创建一个新方法,因为类型不同。也就是说,我个人只会使用一个简单的ORM解决方案,而另一个答案肯定没有错,只是另一个抽象

您将希望使用泛型并将其转换为泛型

我还没有测试这段代码,它更多的是一个指南

private T GetValue<T>(string tableName, colName)
{
   string query = String.Format("SELECT {0} FROM myTable where myTableName = {1}", colName, tableName);
   T result = default(T);
   try
   {
       using(SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["MyDB"].ConnectionString))
         {
          con.Open();
          using (SqlCommand command = new SqlCommand(query, con))
          {
             result = (T)command.ExecuteScalar();
          }
         }
    }
    catch (SqlException ex)
    {
         Console.WriteLine(ex.Message);
    }
 return result;
}
创建SqlManager类

现在你的方法,对于第二件同样的事情:

private string GetPKColumnName(string tableName)
{
    string query = String.Format("",  tableName);

    SqlCommand cmd = new SqlCommand();
    cmd.CommandText = @"SELECT myColName FROM myTable where myTableName = @TableName";

    cmd.Parameters.AddWithValue("@TableName", tableName);

    object result = SqlManager.ExecuteScalar(cmd);

    return result != null ? (int)object: 0;
}

这个问题更适合于我是否应该将返回类型声明为一个很可能不是的对象。定义一个封装名称和长度的类,并返回该类。应使用基于参数的查询,以避免sql注入或其他问题。令人好奇的是,为什么向下投票,它简洁地回答了他提出的问题,没有假设太多这对SQL注入是开放的-我希望应用程序只向方法传递常量,但是在传递要注入的内容的地方构建动态SQL仍然会犯错误或恶意。除了将myTableName参数固定为SQL参数外,安全的方法是执行select*,然后使用SqlDataReader检索适当的值,而无需进行大量额外工作(虽然速度会稍慢)。虽然确实是这样,但您认为这是一个UI吗?我没有读过关于这个UI的任何东西,有一种方法可以随意地基于任意输入构建SQL,这是一种糟糕的模式。否则,如果有人进来编写一些好的单元测试,或者如果它被移植到某个有UI的东西上,那么您就有了一段易受攻击的代码。另一个问题(可能可以通过添加方括号来解决)是,SQL关键字(如From或多字列名)将在此处失败。我想方括号会解决这个问题,但这仍然是另外一个需要考虑的问题。
private string GetPKColumnName(string tableName)
{
    string query = String.Format("",  tableName);

    SqlCommand cmd = new SqlCommand();
    cmd.CommandText = @"SELECT myColName FROM myTable where myTableName = @TableName";

    cmd.Parameters.AddWithValue("@TableName", tableName);

    object result = SqlManager.ExecuteScalar(cmd);

    return result != null ? (int)object: 0;
}