private bool Insert(MODELFILEDOWNLOAD MODELFILEDOWNLOAD) { StringBuilder str = new StringBuilder(); str.Append("INSERT INTO FILEUPLOAD(FILENAME,VERSION,FILECONTENT,OPERATORTIME,FILEPATH) "); str.Append("VALUES(@FILENAME,@VERSION,@FILECONTENT,@OPERATORTIME,@FILEPATH)"); SqlParameter[] para = { new SqlParameter("@FILENAME", SqlDbType.VarChar, 50), new SqlParameter("@VERSION", SqlDbType.VarChar, 50), new SqlParameter("@FILECONTENT", SqlDbType.Image), new SqlParameter("@OPERATORTIME", SqlDbType.DateTime), new SqlParameter("@FILEPATH", SqlDbType.VarChar, 200) }; para[0].Value = MODELFILEDOWNLOAD.FILENAME; para[1].Value = MODELFILEDOWNLOAD.VERSION; para[2].Value = MODELFILEDOWNLOAD.FILECONTENT; para[3].Value = MODELFILEDOWNLOAD.OPERATORTIME; para[4].Value = MODELFILEDOWNLOAD.FILEPATH; if (DBUtility.DbHelperSQL.ExecuteSql(str.ToString(), para) > 0) { return(true); } else { return(false); } }
private void btUpload_Click(object sender, EventArgs e) { if (txtFileName.Text.Trim() == "") { MessageBox.Show("请先点击浏览按钮选择要上传的文件!"); btPreview.Focus(); return; } if (!Information.IsNumeric(txtVersion1.Text)) { MessageBox.Show("版本号只能为整数数字"); txtVersion1.Focus(); return; } if (!Information.IsNumeric(txtVersion2.Text)) { MessageBox.Show("版本号只能为整数数字"); txtVersion2.Focus(); return; } if (!File.Exists(strFullName)) { MessageBox.Show("选择的文件已经删除或者不存在!请重新选择。"); txtFileName.Clear(); return; } if (tvDirectory.SelectedNode == null) { MessageBox.Show("请选择文件的更新路径!"); tvDirectory.Focus(); return; } else { if (tvDirectory.SelectedNode.Tag == null || tvDirectory.SelectedNode.Tag == DBNull.Value) { MessageBox.Show("获取文件的更新路径失败,请重新选择!"); tvDirectory.Focus(); return; } } try { FileInfo fileinfo = new FileInfo(strFullName); MODELFILEDOWNLOAD MODELFILEDOWNLOAD = new MODELFILEDOWNLOAD(); MODELFILEDOWNLOAD.FILENAME = fileinfo.Name; MODELFILEDOWNLOAD.FILEPATH = tvDirectory.SelectedNode.Tag.ToString(); MODELFILEDOWNLOAD.VERSION = txtVersion1.Text + "." + txtVersion2.Text; string strFilter = " AND FILENAME='" + MODELFILEDOWNLOAD.FILENAME + "' AND VERSION='" + MODELFILEDOWNLOAD.VERSION + "'"; if (Query(strFilter).Rows.Count > 0) { MessageBox.Show("该文件已经上传至服务器,无需再次上传。"); txtFileName.Clear(); return; } //if (fileinfo.Extension.ToLower() == ".dll" || fileinfo.Extension.ToLower() == ".exe") //{ // MODELFILEDOWNLOAD.VERSION = FileVersionInfo.GetVersionInfo(strFullName).FileVersion; //} //else // MODELFILEDOWNLOAD.VERSION = "9";//不是DLL或者EXE的文件。 FileStream fs = new FileStream(strFullName, FileMode.Open); MODELFILEDOWNLOAD.FILECONTENT = new Byte[fs.Length]; fs.Position = 0; fs.Read(MODELFILEDOWNLOAD.FILECONTENT, 0, Convert.ToInt32(fs.Length)); MODELFILEDOWNLOAD.OPERATORTIME = GetDatetimeNow(); fs.Close(); if (Insert(MODELFILEDOWNLOAD)) { MessageBox.Show("文件上传成功!"); return; } } catch (Exception ex) { MessageBox.Show(ex.ToString()); } }