2017-07-30 193 views
0

我對此網站不熟悉。我目前正在研究wcf。我試圖從這個艱苦的工作中實現somethig,但我面臨一些問題。我有我的MS Visual Stuido 2015中運行的WCF服務,其中有本地類名爲AccountBalance,這個類有幾個字符串propertys像account_number,account_balace等。我創建的數據庫在MS Sql服務器中。目前,我在控制檯應用程序中使用wcf服務。我試圖通過使用帳號作爲關鍵字來從控制檯窗口中的數據庫中檢索記錄。當我輸入帳號時,它應該在控制檯應用程序中顯示來自數據庫的其餘記錄,但是我收到錯誤,並說我無法運行它。 不能從 '字串' 轉換爲MyService.AccountBalanceRequest」 這裏是從另一個基類繼承的類代碼..在控制檯應用程序中使用WCF服務

  [DataContract] 
     public class AccountBalanceRequest : Current_Account_Details 
      { 
    string account_number; 

    [DataMember] 
    public string Account_Number 
    { 
     get { return account_number; } 
     set { account_number = value; } 
    } 
} 

}

這裏是ADO.NET CODE ..

 public bool AccountBalanceCheek(AccountBalanceRequest accountNumber) 
    { 
     using (SqlConnection conn = new SqlConnection(ConnectionString)) 
     { 
      conn.Open(); 
      using (SqlCommand cmd = new SqlCommand("SELECT * FROM 
      Current_Account_Details WHERE Account_Number ='" + 
       accountNumber.Account_Number + "'", conn)) 
      { 
       cmd.Parameters.AddWithValue("@Account_Number", 
       accountNumber.Account_Number); 
       cmd.CommandType = CommandType.Text; 
       cmd.ExecuteNonQuery(); 
       return true; 

      } 

     } 
    } 

這裏是控制檯應用程序代碼..

 public static void Balance() 
    { 
     MyService.HalifaxCurrentAccountServiceClient currentAccount = new MyService.HalifaxCurrentAccountServiceClient("NetTcpBinding_IHalifaxCurrentAccou 
      ntService"); 

     MyService.AccountBalanceRequest cs = new MyService.AccountBalanceRequest(); 


     string AccountNumber; 


     Console.WriteLine("\nEnter your Account Number--------:"); 
     AccountNumber = Console.ReadLine(); 
     cs.Account_Number = AccountNumber; 
     MyService.AccountBalanceRequest cs1 = 

    currentAccount.AccountBalanceCheek(AccountNumber);//Error on this line. 

       Console.WriteLine("Your Account Number is :" + cs.Account_Number); 
       Console.WriteLine("Your Account Type :" + cs.Account_Balance); 
       Console.WriteLine("Your Account Account Fee :" + cs.Account_Fee); 
       Console.WriteLine("Your Account Balance:" + cs.Account_Balance); 
       Console.WriteLine("Your Account Over Draft Limit :" + cs.Over_Draft_Limit); 

       Console.Write("--------------------------"); 
       Console.ReadLine(); 




    } 

我也嘗試過這種方式。該代碼的確出現了錯誤,但沒有給出預期的結果。這裏是代碼。

public static void Balance() 
    { 
     MyService.HalifaxCurrentAccountServiceClient currentAccount = new MyService.HalifaxCurrentAccountServiceClient("NetTcpBinding_IHalifaxCurrentAccountService"); 
     MyService.AccountBalanceRequest cs = new MyService.AccountBalanceRequest(); 


     string AccountNumber; 


     Console.WriteLine("\nEnter your Account Number--------:"); 
     AccountNumber = Console.ReadLine(); 
     cs.Account_Number = AccountNumber; 
     // MyService.AccountBalanceRequest cs1 = currentAccount.AccountBalanceCheek(AccountNumber); 



     if (currentAccount.AccountBalanceCheek(cs)) 
     { 

       Console.WriteLine("Your Account Number is :" + cs.Account_Number); 
       Console.WriteLine("Your Account Type :" + cs.Account_Balance); 
       Console.WriteLine("Your Account Account Fee :" + cs.Account_Fee); 
       Console.WriteLine("Your Account Balance:" + cs.Account_Balance); 
       Console.WriteLine("Your Account Over Draft Limit :" + cs.Over_Draft_Limit); 

       Console.Write("--------------------------"); 
       Console.ReadLine(); 
       //Console.Clear(); 


     } 

    } 

這裏是輸出圖像。 This is image of the out put

+0

任何建議或解決方案? – Rasel

+1

提示:您將從數據庫讀取的值填充到對象的位置?你正在使用'@ Account_Number',但是你的SQL語句不使用它。 – Subbu

+0

好的。在ado.net和控制檯應用程序中 – Rasel

回答

0

正確的做法是:

currentAccount.AccountBalanceCheek(cs); 

不:

currentAccount.AccountBalanceCheek(AccountNumber); 

由於參數的類型爲MyService.AccountBalanceRequest,不string

我希望對你有所幫助:)

+0

我沒有嘗試,但是當我在控制檯窗口中輸入帳號時,它只有品脫帳號但沒有休息數據庫記錄。謝謝阿里 – Rasel

相關問題