using System;
using System.IO;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Configuration;
public partial class 图片存取: System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {//这里可以是在已经注册的用户显示图片滴,这里为注册,所以去掉
        //string str1 = "server=GJ.;database=图片存取;uid=sa";
        //SqlConnection sql = new SqlConnection(str1);
        //sql.Open();
        //SqlCommand com = new SqlCommand("select p_w_picpath from  相片 where name='11'",sql);
        //SqlDataReader rd = com.ExecuteReader();
        //if (rd.Read())
        //{
        //    this.Image1.ImageUrl = rd.GetString(0);
        //}
        //else
        //    this.Label1.Text = "没有头相 ";
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        string str1 = "server=GJ.;database=图片存取;uid=sa";
        SqlConnection sql = new SqlConnection(str1);
          sql.Open();
        SqlCommand com1 = new SqlCommand("select * from  相片 where name='"+this.TextBox1.Text+"'", sql);
        SqlDataReader rd = com1.ExecuteReader();
        if (rd.Read())//确定用户的唯一性,不能同名
        {
            this.Label1.Text = "此用户名已经被占用,请重新输入";
            this.TextBox1.Text = "";
            rd.Close();
        }
           
        else
        {   rd.Close();
            //下面是为了更新头相是删除原有的.这里只是创建,不用
            //string str = this.Image1.ImageUrl;
            //File.Delete(Server.MapPath(str));//因为要替换头相,所以要删除原来的头相
            try
            {
                if (FileUpload1.PostedFile.FileName.Length == 0)
                {
                    Response.Write("<script>alert(\"上传路径不能为空!\");</script>");
                    return;
                }
                string filename = this.FileUpload1.PostedFile.FileName.Substring(this.FileUpload1.PostedFile.FileName.LastIndexOf("\\") + 1);
                string houzhui = FileUpload1.PostedFile.FileName.Substring(this.FileUpload1.PostedFile.FileName.LastIndexOf(".") + 1);
                if (houzhui == "tif" || houzhui == "bmp" || houzhui == "jpg" || houzhui == "gif")//只允许这几种图片格式的上传
                {   //下面是新增用户资料添加
                    //为新用户增加一个文件夹
                    // 创建目录属于新用户的文件夹,根据新用户的名字命名的文件夹
                    DirectoryInfo d = Directory.CreateDirectory(Server.MapPath("p_w_picpath/" + this.TextBox1.Text));
                    //// d1指向路径p_w_picpath
                    //DirectoryInfo d1 = d.CreateSubdirectory("sixAge1");在这里就表示在绝对路径下p_w_picpath下创建sixAge1
                    FileUpload1.PostedFile.SaveAs(Server.MapPath("p_w_picpath/" + this.TextBox1.Text + "/" + filename));//保存在绝对路径下面的文件夹中
                    Image1.ImageUrl = "p_w_picpath/" + this.TextBox1.Text + "/" + filename;
                    this.Label1.Text = Image1.ImageUrl;
                    //下面是新增用户资料添加到数据库中去                        
                    string st = "insert into 相片 (name,p_w_picpath) values ('" + this.TextBox1.Text + "' ,'" + Image1.ImageUrl + "')";
                    //下面是更新已有资料的用户的相片
                    // string st = "update 相片 set p_w_picpath='" + Image1.ImageUrl + "' where name='11'";//
                    SqlCommand com = new SqlCommand(st, sql);
                    com.ExecuteNonQuery();
                }
                else
                {
                    Response.Write("<script>alert(\"上传文件必须是tif、jpg、bmp、gif格式!\");</script>");
                    return;
                }
            }
            catch (Exception ex)
            {
                Response.Write("<script>alert(\"" + ex.Message + "\");</script>");
            }
        }
    }
}
   添加图片image~_图片添加