Silverlight knowledge [2]: Using Silverlight and WCF to access the MSSQL database

Source: Internet
Author: User
The project needs to access the database through Silverlight and obtain data. However, because Silverlight is running in the Security Sandbox of the browser client, you are not allowed to directly connect to the database, that is, you are not allowed to manage it yourself.
 
Sqlconnectin and other things, but we can use WebService or WCF to connect to the database.

1. First, you can use "new item" in the Silverlight project to create a LINQ to sqldatabase model class, such:

2. Open "server resource manager" and drag the table of the database you want to connect to in the Connected Server to the LINQ to SQL panel created in step 1, for example:

3. Modify the database table namespace as follows:

4. Create a data communication service for the WCF Service through the "new item", such:

5. Set up CS files opened by IDE to perform database operations, the following Code :

// Add more operations here and mark them with [operationcontract] [operationcontract] Public String getimageuri (string keyword, string type) {dataclasses1datacontext Dc = new dataclasses1datacontext (); vaR IMG = from m in DC. key_point2 from N in DC. images from o in DC. key_word from P in DC. key_word_to_image where (O. WORD = keyword) & (O. id = P. key_word_id) & (P. img_id = n. ID) & (M. type = type) Select New {M, N, O, P}; string imgaddress = ""; foreach (VAR t in IMG) {If (T! = NULL) {// return image address 3-25 Ellic imgaddress = T. N. Address. tostring () ;}} return imgaddress;

In this way, we can call this implementation in the back-end code of Silverlight.

6. Right-click the project and select "add service reference". You can click "Discover" to automatically add the previously generated service. If you find that the download fails, you can regenerate the project and then add it. You can modify the namespace name by yourself.

7. The project needs to implement the function of searching for the image information in the database by clicking the button, and then loading the matched image. The front-end XAML is omitted. The key is the back-end call code. It should be noted that it is not called directly through the wtcclinet. getimageuri method, but it is called through the synchronous mechanism, as shown below:

 
/*
* This is added in the response event of the button. Note that the methods in the WCF Service are called through the synchronization mechanism, rather than directly using. */Wtcserviceclient wtcclinet = new wtcserviceclient (); wtcclinet. getimageuricompleted + = new eventhandler <getimageuricompletedeventargs> (wtcclient_get_imageuricompleted); wtcclinet. getimageuriasync (subpre, "subject"); Private void wtcclient_get_imageuricompleted (Object sender, getimageuricompletedeventargs e) {string imguri = E. result. tostring (); popwindow PW = new popwindow (); PW. label1.content = imguri; PW. show ();}

8. The result is as follows: connect to the database and obtain the image address:

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.