日期:2014-05-20  浏览次数:20983 次

.net中怎么做到客户端的自动升级
首先添加一个方法GetVersion

 public string GetVersion(string softname)
  {
   string sVersion = "";
   webmod.dbConnStart(); //(连接)作者自己的连接数据库类,用户自己完成数据库连接
   string strSQL = "select MAX(version) as MaxVerID from chkVersion where softname = @softname";
   SqlCommand sqlCmd = new SqlCommand(strSQL,webmod.sqlConn);
   sqlCmd.CommandTimeout = 0;
   sqlCmd.Parameters.Add("@softname",SqlDbType.VarChar).Value = softname;
   SqlDataReader sqlRd = sqlCmd.ExecuteReader();
   if(sqlRd.HasRows)
   {
    sqlRd.Read();
    sVersion = Convert.ToString(sqlRd["MaxVerID"]);
   }
   sqlRd.Close();
   
   webmod.dbConnEnd(); //(断开连接)作者自己的连接数据库类,用户自己完成数据库连接
   return sVersion;
  }

2、添加下载文件内容的方法DownloadSoft
 public byte[] DownloadSoft(string UserName,string PassWord,string SoftDnldName,string SoftHeightVersion)
  {
   //(连接)作者自己的连接数据库类,用户自己完成数据库连接
   webmod.dbConnStart();
   //检查用户合法性
   bool bMember = CheckAuth(UserName,PassWord);//该WebService内的一个检查用户合法性的函数,用户可以自己完成
   if(!bMember)
   {
    webmod.dbConnEnd();
    return null;
   }
   byte[] b = null;
   //我们取出指定软件名称的最高版本的升级包
   string strSQL = "select olefile from vOleFile where (filename=@softname) and version=@ver";
   SqlCommand sqlCmd = new SqlCommand(strSQL,webmod.sqlConn);
   sqlCmd.CommandTimeout = 0;
   sqlCmd.Parameters.Add("@softname",SqlDbType.VarChar).Value = SoftDnldName;
   sqlCmd.Parameters.Add("@ver",     SqlDbType.VarChar).Value = SoftHeightVersion;
   SqlDataReader sqlRd = sqlCmd.ExecuteReader();
   if(sqlRd.HasRows)
   {
    sqlRd.Read();
    b = (byte[])sqlRd["olefile"];//文件的字节内容
   }
   sqlRd.Close();
   //(断开连接)作者自己的连接数据库类,用户自己完成数据库连接
   webmod.dbConnEnd();
   return b;
  }


程序为什么没反应啊?我这是网上找的,不能实现啊!!!
------解决方案--------------------
按理来说,应该是根据数据库里取出的信息来更新

取出的应该包括程序版本信息以及更新的链接
------解决方案--------------------
不知道楼主想要干什么,这是我以前用到的一些代码

//在你的Button1_click事件中添加如下CODE,主要使用异步调用
 private string svcUser = "";
 private string svcPwd  = "";
 private string svcSoftName = "";
 private string svcCurrVersion = "";
 private string svcDnldFileName = "Test.MSI";//下载下来的文件名,
 private byte[] fbyte = null; //下载后的升级文件的内容
 private void Button1_Click(object sender, System.EventArgs e)
 {
  //读取App.config文件中的配置信息
  svcUser = System.Configuration.ConfigurationSettings.AppSettings["user"]; //需要人证的用户名
  svcPwd = System.Configuration.ConfigurationSettings.AppSettings["pwd"];   //认证密码
  svcSoftName = System.Configuration.ConfigurationSettings.AppSettings["babyRecordSoftName"];//软件名称
  svcCurrVersion = System.Configuration.ConfigurationSettings.AppSettings["Version"];//当前版本号

   try
   {
    AutoUpdateWebSvc.SoftUpdate aSvc = new AutoUpdateWebSvc.SoftUpdate();
    //此处可以改成自己实际应用时的URL,不管WEB引用是动态还是静态,调用都会指向该URL
    aSvc.Url = "http://localhost/babyWebSvc/SoftUpdate.asmx";
    if(Button1.Text.Trim() == "检 查")
    {
     //检查最新版本
     System.AsyncCallback cb = new AsyncCallback(SearchVersionCallBack);//异步回调方法,并检查是否有高版本的升级软件存在
     aSvc.BeginSearchVersion(svcSoftName,cb,aSvc);
    }
    else if(Button1.Text.Trim() == "升 级")
    {
     //开始调用下载服务
     InvokeDownload(); //函数体见下面的CODE
    }
    
   }
   catch(Exception ex)
   {
    MessageBox.Show(ex.Message);
   }
 }

  //检查最新版本的异步回调方法
  private void SearchVersionCallBack(System.IAsyncResult ar)
  {
   if(ar==null)return;
   if(ar.IsCompleted)
   {
    try
    {
     AutoUpdateWebSvc.SoftUpdate aSvc = (AutoUpdateWebSvc.SoftUpdate)ar.AsyncState;