2017-04-24 99 views
-3

我想在不同的变量,每个单元格的值在C#中存储或了解数据是在另一个表银行表对Finger_id数据库中存在..我想将每个单元格的数据库值存储在不同的变量变量中C#?

 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(); 
     } 



     } 
+0

不出所料,MSDN有如何从'SqlDataReader'读取数据的例子:https://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqldatareader。 aspx具体而言,您可以在'reader'中的'IDataRecord'对象上使用数字或字符串索引。 – David

+0

还要注意,您的代码容易受到SQL注入攻击。您应该查看ADO.NET中的“参数化查询”。 – David

+0

C#完全支持_Object Orientated Programming_,所以你可以从你的'reader'中取出每一行并构建一个** Collection ** –

回答

0

reader正在读的记录,你可以得到该记录的值由reader上的IDataRecord接口确定。事情是这样的:

// 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 
+0

如何在决策中逐一使用这些值? if(bids == 1) { BankHblButton.Show(); } if(bids == 2) { BankABLButton.Show(); } –

+0

@ZiaUlhaq:你的意思是在我的第二个例子?那么,*整个集合*将不等于'1'。但是,来自*集合的单个元素*可能会:'如果(出价[0] == 1)' – David

+0

有一件事我想比较图像从数据库.. –

相关问题