WebService (love.net original) for sharing data among complete Web sites

Source: Internet
Author: User
web| Data | Original I remember as if a net friend had asked about how to share database resources between several sites
I tested it on two computers. My code is like this to provide you with reference
There is a datasheet in the database server of site a Noteboard
Contains field ID (number), title (title), notername (message person name), Notetime (Message time)
How can I get site B to get a record of this datasheet?
Define a WebService file to access the a station database in a myviewdbservice.asmx
<% @WebService language= "C #" class= "Viewdbservice"%>
Using System;
Using System.Data;
Using System.Data.OleDb;
Using System.Web.Services;
public class Viewdbservice:webservice
{
[WebMethod]
Public DataSet viewdb ()
{
String connstr= "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=e:\\wmjdb.mdb";
OleDbConnection conn=new OleDbConnection (CONNSTR);
String sqls= "Select Id,title,notername,notetime from Noteboard order by ID";
OleDbDataAdapter adapter=new OleDbDataAdapter ();
Adapter. Selectcommand=new OleDbCommand (Sqls,conn);
DataSet dataset=new DataSet ();
Adapter. Fill (DataSet, "Noteboard");
Conn. Close ();
return dataSet;
}
}
///////////////////////////////////////////////////////////////////////////////
Let's say this webservice in Http://www.a.com/MyViewDBService.asmx.
You can use it as a guest-side in Site B
Wsdl/l:cs/n:dbservice/out:viewdbserviceclient.cs Http://www.w.com/MyViewDBService.asmx
Generating Client Files ViewDBServiceClient.cs
Compiling DLLs with Csc/t:library/out:viewdbserviceclient.dll ViewDBServiceClient.cs

Writing client page Files index.aspx
<% @page language= "C #" codebehind= "Index.aspx.cs" autoeventwireup= "false" inherits= "wmj.viewdb"%>
<title> My message Board </title>
<body>
<form runat= "Server" >
<center>
<asp:datagrid id= "DataGrid1" itemstyle-backcolor= "#AAAADD" autogeneratecolumns= "false"
Alternatingitemstyle-backcolor= "#CCCCFF" headerstyle-backcolor= "#000000"

Headerstyle-horizontalalign= "Center"
Headerstyle-forecolor= "#FFFFFF" pagerstyle-mode= "NumericPages"
Allowpaging= "true" pagesize= "4" font-size= "10pt" runat= "Server" >
<columns>
<asp:boundcolumn headertext= "ordinal" datafield= "ID"/>
<asp:boundcolumn headertext= "title" datafield= "titles"/>
<asp:boundcolumn headertext= "message person" datafield= "Notername"/>
<asp:boundcolumn headertext= "Message Time" datafield= "Notetime" dataformatstring= "{0:dd/mm/yyyy}"/>

</columns>
</asp:DataGrid>
<asp:label id= "Label1" runat= "Server"/>
</center>
</form>
</body>
Codebehind Index.aspx.cs for writing client files
////////////////////////////////////////////////////////////////////

Using System;
Using System.Web.UI;
Using System.Web.UI.WebControls;
Using System.Data;
Using System.Data.OleDb;
Using Dbservice; Introducing the name space of the client file
Namespace WMJ
{
public class Viewdb:page
{
protected DataGrid dataGrid1;
Public viewdb ()
{
This. Init+=new EventHandler (this. Page_Init);
}
public void Page_Init (Object Sender,eventargs e)
{
This. Load+=new EventHandler (this. Page_Load);
This.datagrid1.pageindexchanged+=new

Datagridpagechangedeventhandler (this. datagrid1_pageindexchanged);
}

public void Page_Load (Object Sender,eventargs e)
{
Viewdbservice viewdbservice=new Viewdbservice ();
Using WebService
Datagrid1.datasource=viewdbservice.viewdb (). tables["Noteboard"]. DefaultView;
if (! Page.IsPostBack)
{
datagrid1.currentpageindex=0;
Datagrid1.databind ();
}


}
public void Datagrid1_pageindexchanged (Object Sender,datagridpagechangedeventargs e)
{
Datagrid1.currentpageindex=e.newpageindex;
Datagrid1.databind ();
}
}
}







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.