: LAB Day 1 - Weebly
B?I T?P LABM?c tiêuLàm quen t?o Web Service ??n gi?nVi?t Web Service có t??ng tác d? li?uXu?t b?n Web Service lên InternetS? d?ng Web ServiceCách t?o Web ServiceT?o CSDL trong SQL Server Kh?i ??ng SQL Server Management Studio, m? file DBDemoSrcipt.sql s?n có r?i b?m F5.Vi?t Web Service ?? t??ng tác v?i CSDLT?o m?i Project d?ng Empty Website:C?u trúc Website sau khi t?o:S? d?ng LINQ to SQL k?t n?i t?i CSDLVào menu View ch?n Server ExplorerClick ch?n Connect to Database (hình phích c?m ?i?n)Ch?n Data Source là MS SQL Server (n?u có):?i?n th?ng tin k?t n?i CSDL:Servername: . (d?u ch?m – (local))Ch?n ch? ?? ch?ng th?c “Windows Authentication”Ch?n Database: DBDemoWSK?t qu? sau khi ch?n:T?o m?i file LINQ to SQL, ??t tên DBDemoWS:Kéo th? các ??i t??ng vào t?ng khung t??ng ?ng:?è gi? phím Ctrl + click chu?t vào 2 b?ng ChungLoai và SanPham r?i kéo vào vùng s? 1.K?t qu? sau khi kéo th?:T?o m?i Web ServiceT?o m?i Web Service và ??t tên MyWebService.Ti?n hành thêm m?t s? hàm ?? s? d?ng cho bài lab này nh? sau:Hàm ??m xem có bao nhiêu danh m?c trong b?ng ChungLoaiHàm tr? v? danh sách ChungLoaiHàm tr? v? th?ng tin c?a 1 ChungLoai theo MaLoaiHàm tr? v? danh sách SanPhamHàm tr? v? danh sách SanPham theo MaLoaiHàm tr? v? th?ng tin c?a m?t SanPham theo MaSPHàm xóa ChungLoai theo MaLoaiHàm xóa SanPham theo MaSPCode x? l?:/// <summary>/// MyWebService : L?y th?ng tin s?n ph?m, ch?ng lo?i/// </summary>[WebService(Namespace = "")][WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)] // [System.Web.Script.Services.ScriptService]public class MyWebService : System.Web.Services.WebService { //Khai báo l?nh làm vi?c v?i CSDL DBDemoWSDataContext db = null; public MyWebService () { //Uncomment the following line if using designed components //InitializeComponent(); db = new DBDemoWSDataContext(); } [WebMethod] public string HelloWorld() { return "Hello World"; } //Hàm ??m xem có bao nhiêu danh m?c trong b?ng ChungLoai [WebMethod] public int DemChungLoai() { return db.ChungLoais.Count(); } //2- Hàm tr? v? danh sách ChungLoai [WebMethod] public List<ChungLoai> LayDSChungLoai() { List<ChungLoai> mylist = db.ChungLoais.ToList(); foreach (ChungLoai c in mylist) c.SanPhams.Clear(); return mylist; } //3- Hàm tr? v? th?ng tin c?a 1 ChungLoai theo MaLoai [WebMethod] public ChungLoai LayChungLoai(int maloai) { ChungLoai c = db.ChungLoais.SingleOrDefault(x => x.MaLoai == maloai); c.SanPhams.Clear(); return c; } //4- Hàm tr? v? danh sách SanPham [WebMethod] public List<SanPham> LayDSSanPham() { List<SanPham> dsSP = db.SanPhams.ToList(); foreach (SanPham p in dsSP) p.ChungLoai = null; return dsSP; } //5- Hàm tr? v? danh sách SanPham theo maloai [WebMethod] public List<SanPham> LayDSSanPhamTheoMaLoai(int maloai) { List<SanPham> dsSP = db.SanPhams.Where(x => x.MaLoai == maloai).ToList(); foreach (SanPham p in dsSP) p.ChungLoai = null; return dsSP; } //6- Hàm tr? v? th?ng tin c?a m?t SanPham theo masp [WebMethod] public SanPham LaySanPham(int masp) { SanPham p = db.SanPhams.SingleOrDefault(x => x.MaSP == masp); p.ChungLoai = null; return p; } //7- Hàm xóa ChungLoai theo ID [WebMethod] public bool XoaChungLoai(int maloai) { try { ChungLoai cate = LayChungLoai(maloai); db.ChungLoais.DeleteOnSubmit(cate); db.SubmitChanges(); } catch { return false; } return true; } //8- Hàm xóa SanPham theo ID [WebMethod] public bool XoaSanPham(int masp) { try { SanPham p = LaySanPham(masp); db.SanPhams.DeleteOnSubmit(p); db.SubmitChanges(); } catch { return false; } return true; } //9 -L?y Lo?i tr? v? JSON [WebMethod] [ScriptMethod(ResponseFormat = ResponseFormat.Json)] public string LayDSChungLoaiJSON() { List<ChungLoai> mylist = db.ChungLoais.ToList(); foreach (ChungLoai c in mylist) c.SanPhams.Clear(); return new JavaScriptSerializer().Serialize(mylist); }}Ti?n hành ch?y th? b?ng cách b?m phím F5 trên Web Service ?ang ch?n.??a CSDL và .NET webservice lên hostingS? d?ng Web service?ng d?ng Web?nh x? Webservice ?? s? d?ng:B?m chu?t ph?i vào Project/ ch?n Add Service Reference:Màn hình ch?n WebService hi?n th? ra nh? bên d??i ??y:Sau ?ó ch?n Advanced:B?m Add Reference.S? d?ng<asp:Button ID="btnLayChungLoai" runat="server" Text="L?y Lo?i" OnClick="btnLayChungLoai_Click" /> <asp:Button ID="btnLaySanPham" runat="server" Text="L?y S?n ph?m" OnClick="btnLaySanPham_Click" /> <br /> Lo?i: <asp:DropDownList ID="ddlLoai" runat="server" AutoPostBack="True" OnSelectedIndexChanged="ddlLoai_SelectedIndexChanged"> </asp:DropDownList> <asp:GridView ID="gvResult" runat="server" BackColor="White" BorderColor="#CCCCCC" BorderStyle="None" BorderWidth="1px" CellPadding="3"> <FooterStyle BackColor="White" ForeColor="#000066" /> <HeaderStyle BackColor="#006699" Font-Bold="True" ForeColor="White" /> <PagerStyle BackColor="White" ForeColor="#000066" HorizontalAlign="Left" /> <RowStyle ForeColor="#000066" /> <SelectedRowStyle BackColor="#669999" Font-Bold="True" ForeColor="White" /> <SortedAscendingCellStyle BackColor="#F1F1F1" /> <SortedAscendingHeaderStyle BackColor="#007DBB" /> <SortedDescendingCellStyle BackColor="#CAC9C9" /> <SortedDescendingHeaderStyle BackColor="#00547E" /> </asp:GridView>Code:public partial class DemoLaySanPham : System.Web.UI.Page{ com.somee.hienlthapp.MyWebService test = new com.somee.hienlthapp.MyWebService(); bool isFinish = false; protected void Page_Load(object sender, EventArgs e) { } protected void btnLayChungLoai_Click(object sender, EventArgs e) { List<com.somee.hienlthapp.ChungLoai> listCate = test.LayDSChungLoai().ToList(); gvResult.DataSource = listCate; gvResult.DataBind(); ddlLoai.DataSource = listCate; ddlLoai.DataTextField = "TenLoai"; ddlLoai.DataValueField = "MaLoai"; ddlLoai.DataBind(); } private void LaySanPhamTheoLoai(object sender, EventArgs e) { int masp = int.Parse(ddlLoai.SelectedValue); List<com.somee.hienlthapp.SanPham> dsSP = test.LayDSSanPhamTheoMaLoai(masp).ToList(); gvResult.DataSource = dsSP; gvResult.DataBind(); } protected void btnLaySanPham_Click(object sender, EventArgs e) { List<com.somee.hienlthapp.SanPham> dsSP = test.LayDSSanPham().ToList(); gvResult.DataSource = dsSP; gvResult.DataBind(); } protected void ddlLoai_SelectedIndexChanged(object sender, EventArgs e) { int masp = int.Parse(ddlLoai.SelectedValue); List<com.somee.hienlthapp.SanPham> dsSP = test.LayDSSanPhamTheoMaLoai(masp).ToList(); gvResult.DataSource = dsSP; gvResult.DataBind(); }}S? d?ng WS ? Windows Forms S? d?ng WS ? Android-----------------------------------------------------oOo------------------------------------------------- ................
................
In order to avoid copyright disputes, this page is only a partial summary.
To fulfill the demand for quickly locating and searching documents.
It is intelligent file search solution for home and business.