上传名称将图像更改为数据库

时间:2014-12-28 12:50:14

标签: c# asp.net

我正在使用asp.net c#构建一个社交网站,我在图像上传时出现问题,当我将图像上传到数据库时,它的名称将被更改,并且它保存在我提到的真实路径上。但我不能把它带到数据库表。

这是代码

<asp:FileUpload ID="ProfilePic" runat="server" style="margin-top: -10px" />
<asp:Button ID="Register" runat="server" Text=" Register" 
                        onclick="Button2_Click" ValidationGroup="RegisterGroup" Font-Size="Larger" Height="30px" style="text-align: left; margin-left: 106px" Width="103px" />

这是文件背后的代码

    if (ProfilePic.HasFile)
    {
        if ((ProfilePic.PostedFile.ContentType == "image/jpeg") ||
           (ProfilePic.PostedFile.ContentType == "image/png") ||
           (ProfilePic.PostedFile.ContentType == "image/bmp") ||
           (ProfilePic.PostedFile.ContentType == "image/gif"))
        {

            if (Convert.ToInt64(ProfilePic.PostedFile.ContentLength) < 10000000)
            {
                string photofolder = Path.Combine(@"C:\Users\Supun\Documents\Visual Studio 2013\WebSites\MeetYou\ProfilePic", User.Identity.Name);

                if (!Directory.Exists(photofolder))
                    Directory.CreateDirectory(photofolder);


                string extension = Path.GetExtension(ProfilePic.FileName);
                string uniqueFileName = Path.ChangeExtension(ProfilePic.FileName, DateTime.Now.Ticks.ToString());

                ProfilePic.SaveAs(Path.Combine(photofolder, uniqueFileName + extension));







    // string harshpassword = FormsAuthentication.HashPasswordForStoringInConfigFile(RegPassword.Text, "shar1");

    if (IsPostBack)
    {

        SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["RegistrationConnectionString"].ConnectionString);
        conn.Open();
        string checkemail = "select count(*) from UserData where Email='" + RegEmail.Text + "'";
        SqlCommand com = new SqlCommand(checkemail, conn);
        int temp = Convert.ToInt32(com.ExecuteScalar().ToString());
        if (temp == 1)
        {


            string message = "Email already Exits";
            ClientScript.RegisterStartupScript(this.GetType(), "Popup", "ShowPopup('" + message + "');", true);


        }

        else{

             try{
             //   SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["RegistrationConnectionString"].ConnectionString);
                Guid newGuid = Guid.NewGuid();

                string inserQuery = "insert into UserData(YourName,Email,Password,Gender,Birthday,AboutMe,Country,ID) values (@YName,@REmail,@RPassword,@DDownGender,@MTextBox,@RAboutMe,@DCountry,@ID)";
                SqlCommand comm = new SqlCommand(inserQuery, conn);
                comm.Parameters.AddWithValue("@YName", Your_Name.Text);
                comm.Parameters.AddWithValue("@REmail", RegEmail.Text);
                comm.Parameters.AddWithValue("@RPassword", RegPassword.Text);
                comm.Parameters.AddWithValue("@DDownGender", DropDownGender.SelectedItem.ToString());
                comm.Parameters.AddWithValue("@MTextBox", Birthday.Text);
                comm.Parameters.AddWithValue("@RAboutMe", RegAboutMe.Text);
                comm.Parameters.AddWithValue("@DCountry", DropDownCountry.SelectedItem.ToString());
                comm.Parameters.AddWithValue("@ID", newGuid.ToString());
                comm.Parameters.AddWithValue("@ProfilePic", uniqueFileName);


                comm.ExecuteNonQuery();

                Session["RegEmail"] = RegEmail.Text; 
                Response.Redirect("SecurityQuestion.aspx");

              //  string message = "Registration Complete!!! Please login in";
               // ClientScript.RegisterStartupScript(this.GetType(), "Popup", "ShowPopup('" + message + "');", true);



            }



    catch (Exception ex)    {

        Response.Write("Error -->" + ex.ToString());


                            }
        }
        conn.Close();




}

这是sql代码

CREATE TABLE [dbo].[UserData] (
[YourName]    VARCHAR (50)  NULL,
[Email]       VARCHAR (50)  NOT NULL,
[Password]    VARCHAR (50)  NULL,
[Gender]      VARCHAR (50)  NULL,
[Birthday]    VARCHAR (50)  NULL,
[AboutMe]     VARCHAR (50)  NULL,
[Country]     VARCHAR (50)  NULL,
[ID]          NVARCHAR (50) NOT NULL,
[ProfilePic ] IMAGE         NULL,
CONSTRAINT [PK_UserData] PRIMARY KEY CLUSTERED ([Email] ASC)

);

有些人可以告诉我如何将这个名称更改为数据库表,并告诉我们在数据库表中保存图像的好方法是什么,

现在我用它来保存图像

[ProfilePic ] IMAGE         NULL,

1 个答案:

答案 0 :(得分:0)

我可以看到上面的两个错误。

首先:您没有在INSERT文本中添加ProfilePIC字段,因此添加参数不会执行任何操作。

第二:ProfilePIC字段是一个Image字段,但是如果要存储图像的路径,它应该是正常的NVARCHAR

因此,如果您只想存储已保存图像的完整文件名,则需要使用Sql Server Management Studio更改该列类型,然后将查询文本更改为

 string inserQuery = @"insert into UserData(YourName,Email,Password,Gender,
                                            Birthday,AboutMe,Country,ID, ProfilePic) 
                       values (@YName,@REmail,@RPassword,@DDownGender,
                               @MTextBox,@RAboutMe,@DCountry,@ID, @ProfilePic)";

但是我建议避免使用固定的图像路径。您应该从某种外部配置文件甚至是具有全局选项的数据库表中读取您的个人资料图片的位置。因此,如果您想要移动存储图片的文件夹,则无需更改代码