0

我正在尝试通过 WCF 将窗口电话中的数据插入到 sql server 中。我已经按照这个例子来编辑我的项目..插入数据 但数据没有插入我的数据库。我的编码如下:

放在WindowPhone MainPage.xaml.cs 下。

private void button1_Click(object sender, RoutedEventArgs e)
        {
            //Insert textbox1.text into database
            string empId = textBox1.Text;
            Service1Client proxy = new Service1Client();
            proxy.AddEmployeeCompleted += new EventHandler<AddEmployeeCompletedEventArgs>(proxy_AddEmployeeCompleted);
            proxy.AddEmployeeAsync(empId);
            MessageBox.Show(textBox1.Text);
        }


private void proxy_AddEmployeeCompleted(object sender, AddEmployeeCompletedEventArgs e)
    {
        bool bb = Convert.ToBoolean(e.Result);
        MessageBox.Show(textBox1.Text);
        if (bb == true)
        {
            MessageBox.Show("Record Added Successfully !!");
        }
        else
        {
            MessageBox.Show("Unable to Insert record,Try Again !!");
        }
    }

Service1.svc.cs 处的代码

namespace MyService
{

    public class Service1 : IService1
    {
        public bool AddEmployee(string empId)
        {
            try
            {
                DataClasses1DataContext cont = new DataClasses1DataContext();
                MyEmployee data = new MyEmployee();
                data.EmpId = empId;
                cont.MyEmployees.InsertOnSubmit(data);
                cont.SubmitChanges();
                return true;
            }
            catch
            {
                return false;
            }
        }

    }
}

以及 IService1.cs 中的代码

namespace MyService
{

    [ServiceContract]

    public interface IService1
    {
        [OperationContract]

        bool AddEmployee(string empId);
    }
}

我的编码有问题吗?

4

0 回答 0