如果我不从asp.net中的imageupload控件中选择图像,我想在数据库中插入null

时间:2016-12-02 15:45:23

标签: c# asp.net sql-server

我有一个FileUpload控件,当我不插入图像时,我想将DBNull插入数据库。到目前为止,我只有DBNull.Value的错误。该表允许列ImageData为空。

以下是代码:

protected void button_sign_Click(object sender, EventArgs e)
{
    if (FileUpload1.HasFile == true)
    {
        string str = FileUpload1.FileName;
        FileUpload1.PostedFile.SaveAs(Server.MapPath("~/userimage/" + str));
        string Image = "~/userimage/" + str.ToString();
        string name = username_textbox.Text;
        string email = email_textbox.Text;
        string pass = password_textbox.Text;

        string CS = ConfigurationManager.ConnectionStrings["MyDatabase"].ConnectionString;

        using (SqlConnection con = new SqlConnection(CS))
        {
            SqlCommand cmd = new SqlCommand("insert into Register values(@Username, @Email, @Password, @ImageData)", con);
            cmd.Parameters.AddWithValue("@Username", name);
            cmd.Parameters.AddWithValue("@Email", email);
            cmd.Parameters.AddWithValue("@Password", pass);
            cmd.Parameters.AddWithValue("@ImageData", Image);

            con.Open();
            cmd.ExecuteNonQuery();
            con.Close();

            lblMsg.Text = "Înregistrare cu succes";
            Response.AddHeader("REFRESH", "2;URL=login.aspx");
        }
    }
    else
    {
         lblMsg.Text = "Error";
    }
}

2 个答案:

答案 0 :(得分:2)

这应该足够了

cmd.Parameters.AddWithValue("@ImageData", FileUpload1.HasFile ? Image: DbNull.Value);

还要重构一下你的代码:

string image = "";
if (FileUpload1.HasFile==true)
{
    string str = FileUpload1.FileName;
    FileUpload1.PostedFile.SaveAs(Server.MapPath("~/userimage/" + str));
    image  = "~/userimage/" + str.ToString();
}

string name = username_textbox.Text;
string email = email_textbox.Text;
string pass = password_textbox.Text;

String connString = ConfigurationManager.ConnectionStrings["MyDatabase"].ConnectionString;
using (SqlConnection con = new SqlConnection(connString))
{
    SqlCommand cmd = new SqlCommand("insert into Register values(@Username,@Email,@Password,@ImageData)", con);

    cmd.Parameters.AddWithValue("@Username", name);
    cmd.Parameters.AddWithValue("@Email", email);
    cmd.Parameters.AddWithValue("@Password", pass);
    cmd.Parameters.AddWithValue("@ImageData", FileUpload1.HasFile ? image: DbNull.Value);

    con.Open();
    cmd.ExecuteNonQuery();       
}

lblMsg.Text = "Înregistrare cu succes";
Response.AddHeader("REFRESH", "2;URL=login.aspx");

请勿使用UpperCase字母启动变量。

答案 1 :(得分:1)

如果您在开头设置Image的值,则其余代码可以保持通用。

protected void button_sign_Click(object sender, EventArgs e)
{
    object Image;
    if (FileUpload1.HasFile==true)
    {
        string str = FileUpload1.FileName;
        FileUpload1.PostedFile.SaveAs(Server.MapPath("~/userimage/" + str));
        Image = "~/userimage/" + str.ToString();
    }
    else {
        Image = System.DBNull.Value;
    }

    string name = username_textbox.Text;
    string email = email_textbox.Text;
    string pass = password_textbox.Text;

    String CS = ConfigurationManager.ConnectionStrings["MyDatabase"].ConnectionString;
    using (SqlConnection con = new SqlConnection(CS))
    using(SqlCommand cmd = new SqlCommand("insert into Register values(@Username,@Email,@Password,@ImageData)", con))
    {
        // pick the appropriate SqlDbType type for each parameter
        cmd.Parameters.Add(new SqlParameter("@Username", SqlDbType.VarChar){Value = name});
        cmd.Parameters.Add(new SqlParameter("@Email", SqlDbType.VarChar){Value = email});
        cmd.Parameters.Add(new SqlParameter("@Password", SqlDbType.VarChar){Value = pass});
        cmd.Parameters.Add(new SqlParameter("@ImageData", SqlDbType.VarChar){Value = Image});

        con.Open();
        cmd.ExecuteNonQuery();
        lblMsg.Text = "Înregistrare cu succes";
        Response.AddHeader("REFRESH", "2;URL=login.aspx");
    }

其他一些注意事项

  • 您应该使用参数中的SqlDbType指定数据库类型,以确保ado.net代码正确地翻译了这些值。
  • 在使用区块中包裹命令
  • 无需关闭连接,using块将为您处理。
  • 不要以明文形式存储密码。而是存储密码的盐渍哈希。