2012-04-17 85 views
1

好吧,有一點背景。我發現在C#中使用共享DLL很困難,而且這真的不值得麻煩,因爲這僅僅是一個很好的學校項目,而且我寧願去這條路線。使用Visual Studio C從MS Access檢索數據#

因此,我通過這段代碼將數據存入MS Access。

 public void SetBal(double money) 
    { 
     bal = money; //balance equals whatever money that was sent to it 
     string query = "Insert into Users" + "([Money])" + "Values (@Money)" + "where Users.UserID = 1"; 
     dbconn = new OleDbConnection(connection); 
     OleDbCommand insert = new OleDbCommand(query, dbconn); 
     insert.Parameters.Add("Money", OleDbType.Char).Value = bal; 
     dbconn.Open(); 
     try 
     { 
      int count = insert.ExecuteNonQuery(); 
     } 
     catch (OleDbException ex) 
     { 

     } 
     finally 
     { 
      dbconn.Close(); 
     } 

    } 

好吧,那有效。問題是當我試圖從數據庫中檢索數據時。

public double GetBal() 
    { 
     string query = "SELECT Users.Money FROM Users"; 
     bal = Convert.ToDouble(query); 
     return bal; 
    } 

我無法將查詢結果轉換爲double。我不知道代碼是否錯誤,或者我只是以錯誤的方式去做。提前致謝。

+2

您正在將字符串「SELECT Users.Money FROM Users」加倍,而不是查詢的結果。執行與更新查詢相同的操作,但執行讀取器以獲取所有值 - http://msdn.microsoft.com/ru-ru/library/979byfca.aspx。 – cookieMonster 2012-04-17 03:52:44

+0

謝謝,我什至不知道我怎麼沒有意識到這一點。謝謝回覆! – 2012-04-18 00:26:30

回答

0

您需要創建/打開您的連接,然後執行查詢。你爲插入做了 - 現在你需要使用select查詢在你的get方法中創建同樣的東西。您需要執行查詢,然後將查詢結果轉換爲double。

2
public double GetBal() 
{ 
    // Make sure you change this to a real userID that you pass in. 
    var query = "SELECT Users.Money FROM Users WHERE Users.UserID = 1"; 

    double balance = 0; 

    using (var dbconn = new OleDbConnection(connectionString)) { 
    var command = new OleDbCommand(query, dbconn); 
    dbconn.Open(); 

    // Send the command (query) to the connection, creating an 
    // OleDbReader in the process. We want it to close the database 
    // connection in the process so we pass in that behavior as an 
    // argument (CommandBehavior.CloseConnection) 
    var myReader = command.ExecuteReader(CommandBehavior.CloseConnection); 

    // this while loop will keep executing until there are no more rows 
    // to read from the database. myReader.Read() moves to the next row 
    // in the database too. The first read() puts you at the first row. 
    while(myReader.Read()) 
    { 
     // Use the reader's GetDouble() method to read the data and convert 
     // it to a double. The 0 is there because it is the first column in 
     // the results. for example to read the third column, it would be 
     // myReader.GetDouble(2). 
     balance = myReader.GetDouble(0)); 
    } 
    // because there is only one row (query said where Users.UserID = 1) the 
    // above loop will only execute once. 

    // Close the reader so we can tell the command that the connection 
    // can be closed...because CommandBehavior.CloseConnection was specified 
    myReader.Close(); 
    } 

    // return the value we got from the database 
    return balance; 
} 
+0

嘿,謝謝你的迴應,我非常感謝。我有個問題。我明白了所有的事情,直到嘗試之後,在捕獲之前。你能解釋一下到底發生了什麼嗎?謝謝,我只是想更好地理解代碼。 – 2012-04-18 00:24:57

+1

我添加了一堆評論並簡化了一下。希望這會幫助你理解它。 – 2012-04-18 02:37:59

相關問題