开发者

Silverlight, connecting service.cs and XAML.cs for database

I've found a sample showing how to connect to SQL server (a local MDF), but it uses databinding, how can I use SQL in the normal way (insert, select, update, delete..., sqldatareader, without binding), I think all SQL staff should be performed in service.cs, so how can I use SQL from my XAML.cs? here is the service code:

and here service.cs:

public class ServiceCustomer : IServiceCustomer
{
    public clsCustomer getCustomer(int intCustomer)
    {
        SqlConnection objConnection = new SqlConnection();
        DataSet ObjDataset = new DataSet();
        SqlDataAdapter objAdapater = new SqlDataAdapter();
        SqlCommand objCommand = new SqlCommand("Select * from Customer where CustomerId=" + intCustomer.ToString());
        objConnection.ConnectionString = System.Configuration.ConfigurationManager.ConnectionStrings["ConnStr"].ToString();
        开发者_运维问答objConnection.Open();
        objCommand.Connection = objConnection;
        objAdapater.SelectCommand = objCommand;
        objAdapater.Fill(ObjDataset);
        clsCustomer objCustomer = new clsCustomer();
        objCustomer.CustomerCode = ObjDataset.Tables[0].Rows[0][0].ToString();
        objCustomer.Customer = ObjDataset.Tables[0].Rows[0][1].ToString();
        objConnection.Close();
        return objCustomer;
    }
}

and my page.xaml.cs:

public Page()
    {
        InitializeComponent();
        ServiceCustomerClient obj = new ServiceCustomerClient();
        obj.getCustomerCompleted += new EventHandler<getCustomerCompletedEventArgs>(DisplayResults);
        obj.getCustomerAsync(1);
    }
    void DisplayResults(object sender, getCustomerCompletedEventArgs e)
    {
        LayoutRoot.DataContext = e.Result;

    }

how can I insert a value into my dayabase right from my page.xaml.cs? how can I make a connection between service and xaml objects and functions?

thanks


The Page.xaml.cs file is not autogenerated in a standard silverlight project (beyond it's initial creation via template), so any changes you make to the class will persist.

If your current project does some autogen of xaml classes then you can use the partial class feature of C# Partial Classes in C# Guide to add to the class without worrying that the autogen will remove your additions.


You would need to add another method within your service class that performs the insert, and then pass it the values from your form.

public class ServiceCustomer : IServiceCustomer
{
    public clsCustomer getCustomer(int intCustomer)
    {
      ...
    } 

    public void addCustomer(clsCustomer newCustomer)
    {
         //Code to add the customer to the database
    }
}

to call this from your code you could use the following code, all calls to the service in silverlight are Asynchronous calls therefore they are called in a seperate thread but since the following call has no return there is no need for a completed event handler.

public void foo
{
    clsCustomer cust = new clsCustomer();
    //Create your customer object here

    ServiceCustomerClient obj = new ServiceCustomerClient();

    obj.AddCustomerAsync(cust);
}

this function could then be called when ever you want to add a customer, E.g. when a button is clicked

public void somebutton_Click(object sender, RoutedEventArgs e)
{
      foo();
}

If you need any more information let me know.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜