1.ç¨C#åç®åçWeb Service
ç¨C#åç®åçWeb Service
å¨å¼å§ä¸é¢è¿ä¸ªä¾åä¹åï¼ä½ çç³»ç»éè¦ï¼
1ãWIN + IISï¼
2ãVS.Netï¼
3ãSQL Serverï¼æè¿éç¨çæ¯SQLæ°æ®åºï¼ï¼
è¿ä¸ªWeb Serviceçä¾åç¨çæ¯MS大å¹çC#åçï¼å¦æä½ å欢VBï¼é£ä¹ç¨VBä¹æ¯ä¸æ ·çå¦ï¼åªä¸è¿è¯æ³ä¸ä¸äºå°çå·®å«èå·²ï¼éçé½æ¯ä¸æ ·çï¼ä¸è¿å³ç¶MSé½é¼å¹C#ï¼å¦æä½ è½å¤ç¨C#åè¿æ¯ç¨è¿ä¸ºå¥½å¦ã
ä¸é¢æ¯åçæ¥éª¤ï¼
ä¸ãæå¼VSãNETçéæå¼åç¯å¢ï¼FILEèåä¸éæ©Newï¼æ°å»ºä¸ä¸ªC#çASP.NET Web Serviceå·¥ç¨ï¼å·¥ç¨å为WebServiceDemoï¼å®æ´çæ¯ponents = null;
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this 钱包系统源码下载method with the code editor.
/// </summary>
private void InitializeComponent()
{
}
/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if(disposing && components != null)
{
components.Dispose();
}
base.Dispose(disposing);
}
#endregion
// è¿æ¥å符串常é
const string szConn = "server=(local)\\taoyi;uid=sa;pwd=;"
+ "initial catalog=mydata;data source=taoyi";
[WebMethod]
public String About()
{
return "è¿æ¯ä¸ä¸ªC#ç¼åçWeb Serviceæ¼ç¤ºç¨åº!";
}
// è¿åå ¶ä¸ä¸ä¸ªWebServiceDemo表
[WebMethod]
public DataSet GetServiceDemoTable()
{
DataSet sqlDs = new DataSet();
DataAccess dataAcc = new DataAccess(szConn);
string szSql = "Select * From WebServiceDemo";
sqlDs = dataAcc.GetDataset(szSql,"Demo");
return sqlDs;
}
// è¿åç±ç¨æ·æå®çæ¥è¯¢
[WebMethod]
public DataSet GetByUser(string szCommandText)
{
DataSet sqlDs = new DataSet();
DataAccess dataAcc = new DataAccess(szConn);
sqlDs = dataAcc.GetDataset(szCommandText);
return sqlDs;
}
}
æ¯ä¸æ¯å¾ç®åå¦ï¼å°±åªè¿ä¹ç¹ï¼åµåµ~ï¼ä¸è¿ä¹å¯ä»¥è¯´æé®é¢çäºãè¿ä¸ªç±»ä¸å£°æäºä¸ä¸ªWEBæ¹æ³ï¼æ没æåè§å¢ï¼æ¯ä¸ªæ¹æ³çåé¢é½å äº[WebMethod]ï¼è¡¨ç¤ºè¯¥æ¹æ³ä¸ºWEBæ¹æ³ãåµåµï¼å¦æä½ æ³è¦ä½ åçå½æ°å¯ä»¥è®©WEBåºç¨ç¨åºè°ç¨çè¯ï¼è¿ä¸ªå¯ä¸è½å°çå¦~ï¼ä¸ç¶WEBåºç¨ç¨åºå°±æ æ³è°ç¨çã
å°æ¤ä¸ä¸ªWEBæå¡å°±å®æäºï¼ç¹å»è¿è¡ççï¼å¦æ没ä»ä¹éçè¯ï¼å°±ä¼åºç°å¦ä¸çWEB页é¢æå¡æè¿°ï¼
Service1
The following operations are supported. For a formal definition, please review the Service Description.
* GetByUser
* GetServiceDemoTable
* About
.....ï¼ä¸é¢è¿æå¾å¤ï¼
å ¶ä¸ä»£æå·çå°±æ¯å åå¨å½æ°åå äº[WebMethod]çå½æ°ãå¨åºç°å¨é¡µé¢ä¸ä½ å¯ä»¥åå»ç¸åºçå½æ°ï¼ç¶åå°±ä¼è·³å°è°ç¨é¡µé¢ï¼ä½ å¯ä»¥å¨ç¸åºçææ¬æ¡ä¸ï¼å¦æå½æ°æåæ°çè¯ï¼è¾å ¥ç¸åºçåæ°ï¼ç¹èè°ç¨æé®ï¼é£ä¹å°±å¯ä»¥çå°å½æ°è¿åçç»æäºï¼åææ¯å½æ°è°ç¨æ éçè¯ï¼ï¼ä¸è¿å ¨æ¯XMLæ ¼å¼çææ¬ãæ¯å¦æçGetServiceDemoTableå½æ°è°ç¨çç»æå¦ä¸ï¼
<?xml version="1.0" encoding="utf-8" ?>
- <DataSet xmlns=":xml-msdata">
- <xs:element name="NewDataSet" msdata:IsDataSet="true" msdata:Locale="zh-CN">
- <xs:complexType>
- <xs:choice maxOccurs="unbounded">
- <xs:element name="Demo">
- <xs:complexType>
- <xs:sequence>
<xs:element name="ID" type="xs:int" minOccurs="0" />
<xs:element name="szUser" type="xs:string" minOccurs="0" />
<xs:element name="szSex" type="xs:string" minOccurs="0" />
<xs:element name="szAddr" type="xs:string" minOccurs="0" />
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:choice>
</xs:complexType>
</xs:element>
</xs:schema>
- <diffgr:diffgram xmlns:msdata="urn:schemas-microsoft-com:xml-msdata" xmlns:diffgr="urn:schemas-microsoft-com:xml-diffgram-v1">
- <NewDataSet xmlns="">
- <Demo diffgr:id="Demo1" msdata:rowOrder="0">
<ID>1</ID>
<szUser>taoyi</szUser>
<szSex>ç·</szSex>
<szAddr>åå·æ³¸å·</szAddr>
</Demo>
- <Demo diffgr:id="Demo2" msdata:rowOrder="1">
<ID>2</ID>
<szUser>xiner</szUser>
<szSex>女</szSex>
<szAddr>åå·å®å®¾</szAddr>
</Demo>
</NewDataSet>
</diffgr:diffgram>
</DataSet>
å°æ¤ä¸ºè³ï¼Web Serviceç¨åºå°±å·²ç»ç®æ¯å®æäºã
ä¸é¢è¦åçæ¯åä¸ä¸ªWEBåºç¨ç¨åºæ¥æµè¯æåçè¿ä¸ªWeb Serviceäºï¼ççè½ä¸è½è¾¾å°æ³è¦çç»æã建ç«Webåºç¨ç¨åºçæ¥éª¤å¦ä¸ï¼
ä¸ãæ°å»ºä¸ä¸ªASP.Net Web Applicationå·¥ç¨ï¼ä¸å建Web Serviceç第ä¸æ¥ä¸æ ·ï¼åªæ¯å·¥ç¨ç±»åä¸ä¸æ ·ç½¢äºãæè¿éå·¥ç¨å为WebServiceDemoTest,å®æ´ç为pub\wwwroot\WebserviceDemoTest)ä¸çæäºæéæ件ã
äºãå¨è®¾è®¡è§å¾ä¸æå¼WebForm1.aspxæ件ï¼å¨éé¢æ¾ç½®ä¸ä¸ªpanel容å¨ï¼ä¸ºäºè¾¾å°æµè¯çç®çï¼æ们éè¦ä¸ä¸ªæå¡ç«¯æé®åä¸ä¸ªæå¡ç«¯ææ¬æ¡ï¼åå«è°ç¨æ们å¨Web Serviceä¸åçä¸ä¸ªå½æ°ï¼å¦å¤ï¼ä¸ºäºå±ç¤ºè°ç¨æ¹æ³æå¾è¾¾çæ°æ®ï¼è¿éè¦ä¸ä¸ªæå¡ç«¯æ ç¾æ§ä»¶åä¸ä¸ªDataGridæ§ä»¶ã页é¢çå¸ç½®å°±éä¾¿ä½ äºï¼æ³æä¹æ¾ç½®å°±æä¹æ¾ç½®ï¼åªè¦è½è¾¾å°æ¼ç¤ºçç®çå°±è¡ã
ä¸ãå¼ç¨å ååçWeb Serviceç¨åºï¼èåæ¥éª¤å¦ä¸project->add web reference...ï¼ç¶åè¾å ¥æ们Web Serviceçè·¯å¾ï¼è¿éæ¯http://localhost/WebServiceDemo/Service1.asmxï¼ç¹å»æ·»å å°±OKäºãè¿æ¶ä½ å°å¨ç±»è§å¾ä¸çå°localhostå½å空é´äºã
åãç¼åæµè¯ä»£ç ã
为äºä¾¿äºåé¢å°åäºä»£ç ï¼å¦ï¼xxxx.xxxx.xx xx = new xxxx.xxx.xx()è¿ç§ï¼ï¼é£ä¹é¦å ä½ å¾å¼ç¨localhostå½å空é´çservice1ç±»ï¼ä»¥åç´æ¥åxxxx xx = new xxxx()å°±å¯ä»¥äºã
ä¸é¢æ¯å 个æé®ç代ç ï¼
// æµè¯GetServiceDemoTable()
private void Button2_Click(object sender, System.EventArgs e)
{
DataSet ds = new DataSet();
Service1 oService = new localhost.Service1();
// è¿åè®°å½é
ds = oService.GetServiceDemoTable();
if (ds != null)
{
// æ¾ç¤ºè®°å½éçè®°å½
DataGrid1.DataSource = ds.Tables["Demo"];
DataGrid1.DataBind();
}
else
{
this.Response.Write("å è½½æ°æ®é误!");
}
}
// æµè¯GetByUser()
private void Button1_Click(object sender, System.EventArgs e)
{
DataSet ds = new DataSet();
Service1 oService = new localhost.Service1();
String szCommand = TextBox1.Text;
ds = oService.GetByUser(szCommand);
if (ds != null)
{
DataGrid1.DataSource = ds;
DataGrid1.DataBind();
}
else
Response.Write("é误!æå¯è½æ¯SQLå½ä»¤æé®é¢!");
}
// æµè¯About()
private void Button3_Click(object sender, System.EventArgs e)
{
Service1 oService = new localhost.Service1();
Label1.Text = oService.About();
}
OKï¼æåå°±æ¯è¿è¡äºï¼å¦æä¸åOKï¼ç¹å»ç¬¬ä¸ä¸ªæé®å¾å°çå°æ¯å¨ä¸ä¸ªå å½ç¨æ·æ§è¡çSQLå½ä»¤ç表ç»æã第äºä¸ªæé®å¾å°çå°±æ¯ä¸é¢è¿è¡Web Serviceæ¶çGetServiceDemoTableå¾å°çXMLæè¿°ï¼å³
ID szUser szSex szAddr
1 taoyi ç· åå·æ³¸å·
2 xiner 女 åå·å®å®¾
ç¹å»ç¬¬ä¸ä¸ªæé®ï¼åå¨Label1ä¸æ¾ç¤º"è¿æ¯ä¸ä¸ªC#ç¼åçWeb Serviceæ¼ç¤ºç¨åº!âçå符串ã