2017-07-30 144 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

相关问题