使用控制台应用程序的Wcf服务

时间:2017-08-16 02:56:39

标签: c# sql wcf console

我正在尝试在控制台应用程序中使用Wcf服务。插件,更新和删除操作正由wcf服务与控制台应用程序一起工作,但我无法通过在控制台应用程序中使用wcf服务从数据库中检索数据。我有本地类,它具有所需的数据成员,我在wcf服务中定义了一个方法,该方法需要一个名为帐号的参数。在控制台应用程序用户输入帐号,根据此帐户,控制台应该从sql数据库中检索所有数据,但它只检索我在运行控制台应用程序时提供的帐号。如果有人帮助我,我将不胜感激。

这是本地类的代码..

      [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(cs);//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();

}

这是我运行应用程序时输出的屏幕截图..

Click here to see the out put

1 个答案:

答案 0 :(得分:2)

您的方法AccountBalanceCheek没有从您的数据库中检索数据,因为它使用ExecuteNonQuery并且只返回bool

你能做的是:

    //change return type from bool to AccountBalanceRequest    
    public AccountBalanceRequest AccountBalanceCheek(AccountBalanceRequest accountNumber)
        {
            using (SqlConnection conn = new SqlConnection(ConnectionString))
            {
                conn.Open();
                //use top 1 since you are only getting one record.
                //let us use string interpolation, if you are working below C#6
                //replace it with your previous value
                var cmd = new SqlCommand($@"SELECT TOP 1
                                                * 
                                            FROM 
                                                Current_Account_Details 
                                            WHERE 
                                                Account_Number ='{accountNumber.Account_Number}'", conn));
                cmd.CommandType = CommandType.Text;
                //use ExecuteReader to execute sql select
                //ExecuteNonQuery is for update, delete, and insert.
                var reader = cmd.ExecuteReader();
                //read the result of the execute command.
                while(reader.Read())
                {
                    //assuming that your property is the same as your table schema. refer to your table schema Current_Account_Details
                   //assuming that your datatype are string... just do the conversion...
                    accountNumber.Account_Balance = reader["Account_Balance"].ToString();
                    accountNumber.Account_Fee = reader["Account_Fee"].ToString();
                    accountNumber.Account_Balance = reader["Account_Balance"].ToString();
                    accountNumber.Over_Draft_Limit = reader["Over_Draft_Limit"].ToString();
                }
                return accountNumber;
            }
        }

在您的控制台应用代码中,您必须使用cs1输出您的值。

//previously returns bool, now it returns AccountBalanceRequest
MyService.AccountBalanceRequest cs1 =currentAccount.AccountBalanceCheek(cs);
//use cs1
Console.WriteLine("Your Account Number is :" +cs1.Account_Number);
Console.WriteLine("Your Account Type :" + cs1.Account_Balance);
Console.WriteLine("Your Account Account Fee :" + cs1.Account_Fee);
Console.WriteLine("Your Account Balance:" + cs1.Account_Balance);
Console.WriteLine("Your Account Over Draft Limit :" + cs1.Over_Draft_Limit);