我想将每个单元格的数据库值存储在不同的变量C#中。

时间:2017-04-24 16:27:55

标签: c#

我想将每个单元格值存储在c#中的不同变量中,或者知道数据库中的数据是否存在于另一个表库表中的Finger_id中。

        string queryString = "SELECT b.bank_id  from bank_tbl b  JOIN login_tbl l  ON l.finger_id = b.finger_id WHERE l.passcode = " + pass + " ";
        using (SqlConnection connection = new SqlConnection("Data Source = ZIAULHAQ\\SQLEXPRESS; Initial Catalog = ATM; Integrated Security = True"))
        using (SqlCommand command = new SqlCommand(queryString, connection))
        {
            connection.Open();
            using (SqlDataReader reader = command.ExecuteReader())
            {
                // Call Read before accessing data. 
                if (reader.Read())
                {
                    reader.Read();

                    int bid1 = cell1;
                    int bid2 = cell2;
                    int bid3 = cell3;
                    // Call Close when done reading.
                    reader.Close();
                }
            }
        }
        conn.Close();
if (bid1 == Value)
        {

            BankHblButton.Show();
        }



        }

1 个答案:

答案 0 :(得分:0)

reader正在读取记录时,您可以通过IDataRecord上的reader界面获取该记录的值。像这样:

// declare your variables in the scope where you need them
int bid1 = 0;
int bid2 = 0;
// etc.

if (reader.Read())
{
    bid1 = reader.GetInt32(0);
    bid2 = reader.GetInt32(1);
    // etc.
}
reader.Close();

// You can now use your bid* variables

注意但是当前查询只是从数据库中选择一个值(每行)。因此,与您的问题/代码所暗示的相反,没有“单元格2”或“单元格3”或之后的任何单元格。

如果您打算获取,那么您可以稍微重新构建一下。您可能需要集合而不是单个变量,并且您将使用循环填充它。像这样:

// declare your variables in the scope where you need them
List<int> bids = new List<int>();

while (reader.Read())
{
    bids.Add(reader.GetInt32(0));
}
reader.Close();

// You can now use your bid list