我是这个网站的新手。我目前正在研究wcf。我试图通过这项艰苦的工作来实现一些目标,但我遇到了一些问题。我在我的MS Visual Stuido 2015中运行了WCF服务,它有本地类名为AccountBalance,这个类有几个字符串属性,如account_number,account_balace等。我创建了数据库在MS Sql server中。目前我在控制台应用程序中使用wcf服务。我正在尝试使用帐号作为密钥从控制台窗口中的数据库中检索记录。当我输入帐号时,它应该在控制台应用程序中显示来自数据库的其余记录,但我收到错误说,我无法运行它。 无法从'string'转换为MyService.AccountBalanceRequest' 这是继承自另一个基类的类代码。
[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 :(得分:0)
正确的方法是:
currentAccount.AccountBalanceCheek(cs);
不
currentAccount.AccountBalanceCheek(AccountNumber);
因为参数类型为MyService.AccountBalanceRequest
,而非string
。
我希望对你有所帮助:)。