一段简单WebService应用

using System;
using System.Configuration;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Threading;
using System.Xml;
using System.Data;
using System.Data.SqlClient;
using System.IO;
using WorkFlow.Interface;
using WorkFlow.Common.Data;
using DAL.ApplicationAndTransfer;

[WebService(Namespace 
= "http://tempuri.org/")]
[WebServiceBinding(ConformsTo 
= WsiProfiles.BasicProfile1_1)]
public class Service : System.Web.Services.WebService
{
    
public SecurityHeader currentUser;
    
public Service () {

        
//如果使用设计的组件,请取消注释以下行 
        
//InitializeComponent();
    }


    
protected override void Dispose(bool disposing)
    
{
        
base.Dispose(disposing);
    }

    
/// <summary>
    
/// 把XML文件写入数据库中!!
    
/// </summary>
    
/// <returns>是否成功</returns>

    [WebMethod(Description = "Retrun Excute Result!!True OR False!!"), SoapHeader("currentUser")]
    
public bool WriteXmlDocment()
    
{
        
if (ValidateUser(currentUser.UserName, currentUser.UserPass))
        
{
            SaveXmlFile(currentUser.ConnectionString,currentUser.XmlFilePath,currentUser.bidType);
            
return true;
        }

        
else
        
{
            
return false;
        }

    }

    
/// <summary>
    
/// 写XML文件到数据库的函数
    
/// </summary>
    
/// <param name="conStr">数据库连接字符串</param>
    
/// <param name="filePath">XML文件路径</param>
    
/// <param name="bidType">表单类型</param>

    private void SaveXmlFile(string conStr,string filePath,int bidType)
    
{
        IBid _bid;
        _bid 
= WorkFlow.Client.BidProxy.IBidProxy;
        SqlConnection cn 
= new SqlConnection(conStr);
        FileInfo fi 
= new FileInfo(filePath);
        FileStream fs 
= fi.OpenRead();
        
byte[] bytes = new byte[fs.Length];
        fs.Read(bytes, 
0, Convert.ToInt32(fs.Length));
        fs.Close();
        SqlCommand cm 
= new SqlCommand();
        cm.Connection 
= cn;
        cm.CommandType 
= CommandType.Text;
        cm.CommandText 
= "insert into  XmlFile_Table ( BidID,XmlFile) values(@bidID,@file)";
        SqlParameter bidID 
= new SqlParameter("@bidID",SqlDbType.Char,15);
        bidID.Value 
= _bid.NewBid(bidType);
        _bid.SendBid(
1,1,"","已经签收!!");
        ((IDisposable)_bid).Dispose();
        cm.Parameters.Add(bidID);
        SqlParameter spFile 
= new SqlParameter("@file", SqlDbType.Image);
        spFile.Value 
= bytes;
        cm.Parameters.Add(spFile);
        cn.Open();
        cm.ExecuteNonQuery();
        cn.Close();
    }

    
/// <summary>
    
/// 验证用户
    
/// </summary>
    
/// <param name="userName">用户名</param>
    
/// <param name="userPass">用户密码</param>
    
/// <returns>验证结果</returns>

    private bool ValidateUser(string userName, string userPass)
    
{
        User_Table userTable 
= new User_Table();
        
if (userTable.IDValue(userName, userPass) > 0)
            
return true;
        
else
            
return false;
    }

}

/// <summary>
/// 继承SoapHeader,把用户名,密码,连接字符串,文件路径,表单类型放在SOAP头中
/// </summary>

public class SecurityHeader : SoapHeader
{
    
public string UserName;
    
public string UserPass;
    
public string ConnectionString;
    
public string XmlFilePath;
    
public int bidType;
}

posted @ 2008-06-26 15:05  GuoyingXiao  阅读(645)  评论(0编辑  收藏  举报