工具:VS2010
数据库:Oracle
编码:C#
详细步骤及编码:
1、打开VS2010 ——> 新建项目 ——> ASP.NER空Web应用程序
2、右键工程名——>添加项——>Web服务
3、在Web.config配置文件中的configuration节点下增加AppSettings的配置
<configuration>
<system.web>
<compilation debug="true" targetFramework="4.0" />
</system.web>
<appSettings>
<!--数据库连接设置-->
<add key="OracleConStr" value="Data Source=ip:port/orcl;User Id=XXX;Password=***"/>
</appSettings>
</configuration>
4、编码【查询数据库数据集,返回xml】
#region 数据库操作
#region 打开数据库连接
/// <summary>
/// 打开数据库连接
/// </summary>
private void open(OracleConnection conn)
{
if (conn.State == ConnectionState.Closed)
{
conn.Open();
}
else if (conn.State == ConnectionState.Broken)
{
conn.Close();
conn.Open();
}
}
#endregion
#region 关闭数据库连接
private void Close(OracleConnection conn)
{
//判断连接的状态是否已经打开
if (conn != null && conn.State == ConnectionState.Open)
{
conn.Close();
}
}
#endregion
private void OracleInit()
{
conn = new OracleConnection(OracleConStr);
open(conn);
}
private void OracleClose()
{
Close(conn);
}
#endregion
/// <summary>
/// Oracle连接字符串
/// </summary>
private static string OracleConStr = System.Configuration.ConfigurationSettings.AppSettings["OracleConStr"];
/// <summary>
/// Oracle连接
/// </summary>
private OracleConnection conn = null;
[WebMethod(Description="WebService测试接口")]
public string QueryVital(string str1, string str2)
{
DataSet ds = new DataSet();
DataTable dtTable = new DataTable();
string sql = "";
try
{
OracleInit();
if (str1.ToString().Trim().Length > 0 && str2.ToString().Trim().Length > 0)
{
sql = string.Format(@"SELECT * FROM.......");
}
else if (str1.ToString().Trim().Length > 0 && str2.ToString().Trim().Length == 0)
{
sql = string.Format(@"SELECT * FROM ......");
}
else if (str1.ToString().Trim().Length == 0 && str2.ToString().Trim().Length == 0)
{
sql = string.Format(@"SELECT * FROM ........");
}
else
{
conn.Close(); //关闭连接
return null;
}
OracleCommand cmd = new OracleCommand(sql, conn);
OracleDataAdapter oda = new OracleDataAdapter(cmd);
oda.Fill(dtTable);
}
catch (Exception ex)
{
log.Write(ex.Message + "\r\n", DataSyncWebAppService.MyLog.MsgType.Error);
}
finally
{
OracleClose();
}
return DataTableToXml(dtTable, "TABLE");
}
}
#region DataTableToXml
/// <summary>
/// 将DataTable对象转换成XML字符串
/// </summary>
/// <param name="ds">DataSet对象</param>
/// <returns>XML字符串</returns>
public static string DataTableToXml(DataTable dt, string sName)
{
if (dt != null)
{
MemoryStream ms = null;
XmlTextWriter XmlWt = null;
try
{
ms = new MemoryStream();
//根据ms实例化XmlWt
XmlWt = new XmlTextWriter(ms, System.Text.Encoding.Unicode);
//获取ds中的数据
dt.TableName = sName;
dt.WriteXml(XmlWt, XmlWriteMode.WriteSchema);
int count = (int)ms.Length;
byte[] temp = new byte[count];
ms.Seek(0, SeekOrigin.Begin);
ms.Read(temp, 0, count);
//返回Unicode编码的文本
System.Text.UnicodeEncoding ucode = new System.Text.UnicodeEncoding();
string returnValue = ucode.GetString(temp).Trim();
return returnValue;
}
catch (System.Exception ex)
{
throw ex;
}
finally
{
//释放资源
if (XmlWt != null)
{
XmlWt.Close();
ms.Close();
ms.Dispose();
}
}
}
else
{
return "";
}
}
#endregion
5、发布、测试
(1) 右键项目名称——发布
(2) 将发布的文件部署到IIS,并转换为应用程序
(3) 浏览器访问
(4) XML结果集显示
最后直接在项目中引用即可。