Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/288.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# - Fatal编程技术网

C# 列表<;字符串>;分配给单独的数组

C# 列表<;字符串>;分配给单独的数组,c#,C#,我认为这是矫枉过正,但我想问一下。下面的代码正在运行,是否需要将tempList的值分配给一个完全独立的数组?我觉得您可以迭代tempList的值,但在我进行更改之前需要验证: List<string> tempList = new List<string>(); SqlDataReader reader = cmd.ExecuteReader(); while (reader.Read()) { try { if (!reader.IsDBNull(0

我认为这是矫枉过正,但我想问一下。下面的代码正在运行,是否需要将tempList的值分配给一个完全独立的数组?我觉得您可以迭代tempList的值,但在我进行更改之前需要验证:

List<string> tempList = new List<string>();
SqlDataReader reader = cmd.ExecuteReader();

while (reader.Read())
{
  try
  {
    if (!reader.IsDBNull(0))
    {
      tempList.Add(reader[0].ToString());
    }
  }
  catch
  {
  }
}

reader.Close();
crMy = tempList.ToArray();

for (int q = crMy.GetLowerBound(0); q <= crMy.GetUpperBound(0); q++)
{
  return crMy[q];
}
List templast=newlist();
SqlDataReader=cmd.ExecuteReader();
while(reader.Read())
{
尝试
{
如果(!reader.IsDBNull(0))
{
添加(读取器[0].ToString());
}
}
抓住
{
}
}
reader.Close();
crMy=templast.ToArray();

对于(int q=crMy.GetLowerBound(0);q否,您不需要数组,您的代码可以更改为:

return tempList.First();

first或default
取决于下面的代码。

否您不需要数组,您的代码可以更改为:

return tempList.First();

first或default
,具体取决于下面的代码。

我相信您是正确的,在这种特定上下文中没有理由将列表复制到数组中

我建议对SqlDataReader使用using语句,因为它是一次性对象,我在这里没有看到任何dispose代码

using (SqlDataReader reader = cmd.ExecuteReader())
{
    while (reader.Read())
    {
        try { if (!reader.IsDBNull(0)) { tempList.Add(reader[0].ToString()); } }
        catch { }
    }
}

我相信您是正确的,在这种特定的上下文中没有理由将列表复制到数组中

我建议对SqlDataReader使用using语句,因为它是一次性对象,我在这里没有看到任何dispose代码

using (SqlDataReader reader = cmd.ExecuteReader())
{
    while (reader.Read())
    {
        try { if (!reader.IsDBNull(0)) { tempList.Add(reader[0].ToString()); } }
        catch { }
    }
}

不,不需要初始化新数组。您只需在
列表上迭代即可。
尽管您甚至需要一个列表,因为您只是返回列表中的第一个元素。这将阻止您读取每一行,例如,如果您有一百万个条目,则必须在返回第一行之前读取所有条目

此代码读取并直接返回第一个非空值:

string result = null;
using(var reader = cmd.ExecuteReader()){
    while (reader.Read()) {
        if (!reader.IsDBNull(0)) { 
            return resultreader[0].ToString(); 
    }
}

不,不需要初始化新数组。您只需在
列表上迭代即可。
尽管您甚至需要一个列表,因为您只是返回列表中的第一个元素。这将阻止您读取每一行,例如,如果您有一百万个条目,则必须在返回第一行之前读取所有条目

此代码读取并直接返回第一个非空值:

string result = null;
using(var reader = cmd.ExecuteReader()){
    while (reader.Read()) {
        if (!reader.IsDBNull(0)) { 
            return resultreader[0].ToString(); 
    }
}

编写时,您的代码返回第一行的第一列,其中该列为非空。您可以简单地编写该列并完全避免该列表:

static string GetDataValue()
{
  using ( SqlConnection conn = new SqlConnection("your-connect-string-here"))
  using ( SqlCommand    cmd  = conn.CreateCommand() )
  {
    InitializeSqlCommand(cmd) ;
    conn.Open() ;
    using ( SqlDataReader reader = cmd.ExecuteReader() )
    {
      while ( reader.Read() && reader.IsDBNull( 0 ) )
      {
        string value = reader[0].ToString() ;
        return value ;
      }
    }
  }
  throw new InvalidOperationException("no [useful] data returned");
}
这种方法对我来说似乎有点似是而非:您可能会返回大量数据,通过网络读取所有数据,然后丢弃所有数据,除了第一行的第一列(该列为非空)

您应该修改查询以返回您想要的内容,因此应改为如下内容(相当开放):

将其更改为更具体的:

select top 1 t.col_1
from foo t
where t.col_1 is not null
您的RDBMS会感谢您。您的网络会感谢您。您的代码会运行得更快,您的客户会更高兴,并且您可以用非常简单和更有意的方式替换查询逻辑:

static string GetDataValue()
{
  string value ;
  using ( SqlConnection conn = new SqlConnection("your-connect-string-here"))
  using ( SqlCommand    cmd  = conn.CreateCommand() )
  {
    InitializeSqlCommand(cmd) ;
    conn.Open() ;
    value = (string) cmd.ExecuteScalar() ;
    conn.Close() ;
  }
  if ( value == null ) throw new InvalidOperationException("no data read") ;
  return value ;
}

编写时,您的代码返回第一行的第一列,其中该列为非空。您可以简单地编写该列并完全避免该列表:

static string GetDataValue()
{
  using ( SqlConnection conn = new SqlConnection("your-connect-string-here"))
  using ( SqlCommand    cmd  = conn.CreateCommand() )
  {
    InitializeSqlCommand(cmd) ;
    conn.Open() ;
    using ( SqlDataReader reader = cmd.ExecuteReader() )
    {
      while ( reader.Read() && reader.IsDBNull( 0 ) )
      {
        string value = reader[0].ToString() ;
        return value ;
      }
    }
  }
  throw new InvalidOperationException("no [useful] data returned");
}
这种方法对我来说似乎有点似是而非:您可能会返回大量数据,通过网络读取所有数据,然后丢弃所有数据,除了第一行的第一列(该列为非空)

您应该修改查询以返回您想要的内容,因此应改为如下内容(相当开放):

将其更改为更具体的:

select top 1 t.col_1
from foo t
where t.col_1 is not null
您的RDBMS会感谢您。您的网络会感谢您。您的代码会运行得更快,您的客户会更高兴,并且您可以用非常简单和更有意的方式替换查询逻辑:

static string GetDataValue()
{
  string value ;
  using ( SqlConnection conn = new SqlConnection("your-connect-string-here"))
  using ( SqlCommand    cmd  = conn.CreateCommand() )
  {
    InitializeSqlCommand(cmd) ;
    conn.Open() ;
    value = (string) cmd.ExecuteScalar() ;
    conn.Close() ;
  }
  if ( value == null ) throw new InvalidOperationException("no data read") ;
  return value ;
}

你想在这里实现什么?你实际上不是在迭代。你只是返回模板列表的第一个值。将第一个列表分配给一个辅助crMy数组是否过火?你想在这里实现什么?你实际上不是在迭代。你只是返回模板列表的第一个值。将第一个列表分配给一个se是否过火第二个crMy数组?很漂亮-我认为这是杀伤力过大,但我想确认b4做出的改变让我大吃一惊。如果(templast.Any())返回templast.First(),应该是
if(templast.Any())
匹配当前行为。漂亮-我认为这太过分了,但我想确认b4做出的更改在我脸上爆炸了。如果(templast.Any())返回templast.First();
匹配当前行为,可能应该是