本文介绍了使用SqlDataReader rdr(1)的麻烦的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

错误代码:

'rdr(1)'是'变量',但用作'方法'



ftpTable的第二列。

Error Code:
'rdr(1)' is a 'variable' but is used like a 'method'

The second column of "ftpTable".

try
{
   string connStr3 = ConfigurationManager.ConnectionStrings["FTPdb"].ConnectionString;
   string cmdStr3 = "SELECT * FROM [ftpTable];";
   using (SqlConnection conn3 = new SqlConnection(connStr3))
   {
      using (SqlCommand cmd3 = new SqlCommand(cmdStr3, conn3))
      {
         conn3.Open();
         using (SqlDataReader rdr = cmd3.ExecuteReader())
         {
            while (rdr.Read())
            {
               if (rdr(1).ToString() == DropDownList2.Text)
               {
                  goto match;
               }
            }
         }
         cmd3.Dispose();
         conn3.Close();
         conn3.Dispose();
      }
   }
}
catch (Exception ex) { Label1.Text = "Search SELECT for Match: " + ex.ToString(); }

推荐答案


bool matchFount = false;
string connStr3 = ConfigurationManager.ConnectionStrings["FTPdb"].ConnectionString;
string cmdStr3 = "SELECT * FROM [ftpTable];";
using (SqlConnection conn3 = new SqlConnection(connStr3))
using (SqlCommand cmd3 = new SqlCommand(cmdStr3, conn3))
{
    conn3.Open();
    using (SqlDataReader rdr = cmd3.ExecuteReader())
    {
        while (rdr.Read())
        {
            if (rdr[1].ToString() == DropDownList2.Text)
            {
                matchFount = true;
                break;
            }
        }
    }
}

//some code ...

if (matchFount)
{
    // do something , maching record found 
}



但是如果你需要检查数据库中是否存在匹配记录。您可以将一些参数发送到数据库并直接获取记录计数如下。




but again if you need to check whether matching record exist or not in the database. you can send some parameters to database and get the record count directly like below.

bool matchFount = false;
string connStr3 = ConfigurationManager.ConnectionStrings["FTPdb"].ConnectionString;
string cmdStr3 = "SELECT count(*) FROM [ftpTable] where [secondColName] =@colVal";
using (SqlConnection conn3 = new SqlConnection(connStr3))
using (SqlCommand cmd3 = new SqlCommand(cmdStr3, conn3))
{
    cmd3.Parameters.AddWithValue("@colVal", DropDownList2.Text);
    conn3.Open();
    int? rowcount = (int?)cmd3.ExecuteScalar();
    matchFount = rowcount > 0;
}


这篇关于使用SqlDataReader rdr(1)的麻烦的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

11-01 13:00