商讯信箱
用户名: @
密  码:   注册|忘记密码
登录
个人用户经销商
您的位置:首页 > 技术频道 > 正文

Delphi调用WebServices(C#)代码

作者:佚名  2007-09-11


【IT168 技术文档】

using System; using System.Web; using System.Web.Services; using System.Web.Services.Protocols; using System.Data; using System.Data.OracleClient; using System.IO; using System.IO.Compression; using System.Runtime.Serialization.Formatters.Binary; [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)] public class Service : System.Web.Services.WebService { private OracleConnection webConnection; DataTable curDt = new DataTable(); DataSet curSet = new DataSet(); OracleDataAdapter curDa = new OracleDataAdapter(); OracleCommand curComm = new OracleCommand(); private string GetCurCnnStr() { //连接数据库并打开 OracleConnectionStringBuilder cnnStrBuilder = new OracleConnectionStringBuilder(); cnnStrBuilder.UserID = ""; cnnStrBuilder.Password = ""; cnnStrBuilder.DataSource = ""; return cnnStrBuilder.ConnectionString; } public Service() { //如果使用设计的组件,请取消注释以下行 //InitializeComponent(); } [WebMethod(Description = "判断空值")] private OracleParameter CreateParam(string ParamName, object ParamValue) { OracleParameter Result = new OracleParameter(); Result.ParameterName = ParamName; if (ParamValue != null) { Result.Value = ParamValue; } else { Result.Value = DBNull.Value; } return Result; } [WebMethod(Description = "测试连接")] public string Linking() { return "WebService连接成功!"; } [WebMethod(Description = "查询信息")] public byte[] Search(string img) { try { webConnection = new OracleConnection(GetCurCnnStr()); webConnection.Open(); //查询表中的所有的数据 OracleDataAdapter webAdapter = new OracleDataAdapter("select pic from pic where id=" + img, webConnection); DataSet webDataTable = new DataSet(); //byte[] webDataTable = new byte[0]; webAdapter.Fill(webDataTable); webConnection.Close(); //序列化为二进制 webDataTable.RemotingFormat = SerializationFormat.Binary;//确定序列化格式 BinaryFormatter bFormatter = new BinaryFormatter(); MemoryStream mStream = new MemoryStream(); bFormatter.Serialize(mStream, webDataTable); byte[] bytes = mStream.ToArray();//将数据流写入字节数组 //返回数组 return bytes; } catch (Exception) { curComm.Transaction.Rollback(); return null; } finally { webConnection.Close(); } } [WebMethod(Description = "添加信息")] public void Add(string id, byte[] Image) { try { webConnection = new OracleConnection(GetCurCnnStr()); curComm.Connection = webConnection; webConnection.Open(); curComm.Transaction = webConnection.BeginTransaction(); curComm.CommandText = "insert into pic(id,pic)values(:pId,:pPic)"; curComm.Parameters.Add(CreateParam("pId", (id != null) ? id : null)); curComm.Parameters.Add(CreateParam("pPic", (Image != null) ? Image : null)); curComm.ExecuteNonQuery(); curComm.Transaction.Commit(); } catch (Exception) { curComm.Transaction.Rollback(); } finally { webConnection.Close(); } } }

1 2
【内容导航】
第1页: C#源码 第2页: delphi调用webservice源码}

©版权所有。未经许可,不得转载。
[责任编辑:李宁]
[an error occurred while processing this directive]