ASP.NET 上传图片到服务器并查询

来源:互联网 发布:二手车淘宝 编辑:程序博客网 时间:2024/06/04 19:56

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using System.IO;

public partial class DriverInfo : System.Web.UI.Page
{
    string FilePath ="~//Photo//";
    SqlConnection conn = new SqlConnection(System.Configuration.ConfigurationManager.AppSettings["strcon"]);

 public string UploadPhoto()//上传照片到指定目录
    {
        string stringPhoto = System.DateTime.Now.ToString("yyyymmddhhMMssfff") + ".jpg";//取系统当前时间微秒为上传文件名称
        if (FileUploadPhoto.HasFile)
        {
            FileUploadPhoto.SaveAs(Server.MapPath(FilePath.ToString().Trim() + stringPhoto));//FilePath.ToString().Trim()+ stringPhoto);//保存司机图片
            return stringPhoto;
        }
        else
        {
            Response.Write("<script>alert('没有选择司机照片');history.back()</script>");
            return null;
        }

    }

  public bool GetDriverInfo()//查询司机信息是否已经存在
    {
        SqlConnection conn = new SqlConnection(System.Configuration.ConfigurationManager.AppSettings["strcon"]);
        conn.Open();
        string StrGetDriver = "SELECT SJID FROM DriverInfo where SJID='" + TxtSJID.Text.ToString().Trim() + "'";
        SqlCommand comm = new SqlCommand(StrGetDriver, conn);
        SqlDataReader dr = comm.ExecuteReader();
        if (dr.Read())
        {
            Response.Write("<script>alert('该司机信息已经存在!')</script>");
            conn.Close();
            return false;
        }
        else
        {
            conn.Close();
            return true;
           
        }
       
    }
 protected void UpdatePhoto_Click(object sender, EventArgs e)
    {
         SqlConnection conn = new SqlConnection(System.Configuration.ConfigurationManager.AppSettings["strcon"]);//建立数据源
        if (FileUploadPhoto.HasFile)
       {
           string GetPhotoName = UploadPhoto();//取得照片名字,并上传照片
           try
           {
               conn.Open();
            SqlCommand UpdateDriverPhoto = new SqlCommand("UpdateDriverPhoto",conn);
            UpdateDriverPhoto.CommandType = CommandType.StoredProcedure;
            UpdateDriverPhoto.Parameters.Add("@SJID",SqlDbType.Char);//司机编码
            UpdateDriverPhoto.Parameters.Add("@NewSJPhoto", SqlDbType.Char);//新司机照片
            UpdateDriverPhoto.Parameters.Add("@OldSJPhoto",SqlDbType.Char,40);//旧司机照片
            UpdateDriverPhoto.Parameters["@SJID"].Value = TxtSJID.Text.ToString().Trim();
            UpdateDriverPhoto.Parameters["@NewSJPhoto"].Value = GetPhotoName.Trim();
            UpdateDriverPhoto.Parameters["@OldSJPhoto"].Direction = ParameterDirection.Output;//输出参数
            UpdateDriverPhoto.ExecuteNonQuery();//更新照片信息


           string Message = "成功更新司机照片" + TxtSJID.Text.ToString().Trim();//消息内容
            string UserName = Session["username"].ToString().Trim();
            ClassDataProcess.AddAudit(UserName, Message.Trim());//增加审计信息
            //string OldSJPhoto = UpdateDriverPhoto.Parameters["@OldSJPhoto"].Value.ToString().Trim();//老照片名称


            /*string FileName = FilePath.ToString().Trim() + OldSJPhoto.Trim();//上传图片服务器路径
               if (FileName!=null)
               {
            System.IO.File.Delete(FileName);
               }*/
               Response.Write("<script>alert('司机照片更改成功!')</script>");
              }
             
           catch (SqlException ex)
           {
               throw ex;//抛出异常
           }
           finally
           {
               conn.Close();
           }
       }
    
    }

}

 

原创粉丝点击