0

试图将文本和图像都插入数据库。唯一的必填字段是类别、名称和描述。如果只有这些字段有条目,则插入到数据库中是不成功的。如果加载了不需要的 RecipePicture,则插入成功。我正在使用 SqlServer。

问题必须在后面的代码中,但我找不到它。任何帮助将不胜感激,因为我需要能够在不插入图片/图像的情况下插入数据库。后面的代码:

 protected void UploadButton_Click(object sender, EventArgs e)
        {
            if (FileUpload1.PostedFile != null
            && FileUpload1.PostedFile.FileName != "")
            {

                byte[] myimage = new byte[FileUpload1.PostedFile.ContentLength];
                HttpPostedFile Image = FileUpload1.PostedFile;
                Image.InputStream.Read(myimage, 0,   (int)FileUpload1.PostedFile.ContentLength);

                SqlConnection myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["AsianConnectionString"].ConnectionString);

                SqlCommand storeimage = new SqlCommand("INSERT INTO AsianRecipe"
                 + "(Category, Name, Description, RecipePicture, RecipePictureType, RecipePictureSize, UserName, UserPicture, UserPictureType, UserPictureSize) "
                 + " values (@Category, @Name, @Description, @image, @imagetype, @imagesize, @UserName, @userpicture, @userpicturetype, @userpicturesize)", myConnection);
                storeimage.Parameters.Add("@image", SqlDbType.VarBinary, myimage.Length).Value = myimage;
                storeimage.Parameters.Add("@imagetype", SqlDbType.VarChar, 100).Value
                = FileUpload1.PostedFile.ContentType;
                storeimage.Parameters.Add("@imagesize", SqlDbType.BigInt, 99999).Value
                = FileUpload1.PostedFile.ContentLength;
                storeimage.Parameters.Add("@category", SqlDbType.NVarChar, 50).Value
                = lblSelection.Text;
                storeimage.Parameters.Add("@name", SqlDbType.NVarChar, 100).Value
                = TextBox2.Text;
                storeimage.Parameters.Add("@description", SqlDbType.NVarChar, 250).Value
                = TextBox3.Text;
                storeimage.Parameters.Add("@username", SqlDbType.NVarChar, 50).Value
                = TextBox4.Text;
                storeimage.Parameters.Add("@userpicture", SqlDbType.VarBinary, myimage.Length).Value = myimage;
                storeimage.Parameters.Add("@userpicturetype", SqlDbType.VarChar, 100).Value
                = FileUpload2.PostedFile.ContentType;
                storeimage.Parameters.Add("@userpicturesize", SqlDbType.BigInt, 99999).Value
                = FileUpload2.PostedFile.ContentLength;




                myConnection.Open();
                storeimage.ExecuteNonQuery();
                myConnection.Close();

            }
        }

        protected void OnSelect(object sender, EventArgs e)
        {
            lblSelection.Text = ((LinkButton)sender).Text;
        }
    }
}
4

2 回答 2

0

我认为最好的方法是创建一个具有可选参数的存储过程,然后在您的代码中,您只需添加需要并具有价值的参数。否则,您将不得不一起生成 sql 和参数,恕我直言,这会有点混乱。

我建议你(1)创建一个像这样的sproc

CREATE PROCEDURE dbo.MyProc
(
@image              VarBinary = NULL,
@imagetype          VarChar(100) = NULL,
@imagesize          BigInt = NULL,
@category           NVarChar(50),
@name               NVarChar(100),
@description        NVarChar(50),
@username           NVarChar(50) = NULL,
@userpicture        VarBinary = NULL,
@userpicturetype    VarChar(100) = NULL,
@userpicturesize    BigInt = NULL
)

AS

SET NOCOUNT ON

    INSERT INTO AsianRecipe(Category, Name, [Description], RecipePicture, RecipePictureType, RecipePictureSize, UserName, UserPicture, UserPictureType, UserPictureSize)
    VALUES (@category, @name, @description, @image, @imagetype, @imagesize, @username, @userpicture, @userpicturetype, @userpicturesize)

然后(2)像这样更改您的UploadButton_Click方法,请注意,我已将执行包含在 try/finally 中,以确保连接已关闭,如果您愿意,也可以使用using语句

protected void UploadButton_Click(object sender, EventArgs e)
{
    if (FileUpload1.PostedFile != null && FileUpload1.PostedFile.FileName != "")
    {

        byte[] myimage = new byte[FileUpload1.PostedFile.ContentLength];
        HttpPostedFile Image = FileUpload1.PostedFile;
        Image.InputStream.Read(myimage, 0, (int)FileUpload1.PostedFile.ContentLength);

        SqlConnection myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["AsianConnectionString"].ConnectionString);

        SqlCommand storeimage = new SqlCommand("dbo.MyProc", myConnection);
        if (myimage != null) { storeimage.Parameters.Add("@image", SqlDbType.VarBinary, myimage.Length).Value = myimage; }
        if (!string.IsNullOrEmpty(FileUpload1.PostedFile.ContentType)) { storeimage.Parameters.Add("@imagetype", SqlDbType.VarChar, 100).Value = FileUpload1.PostedFile.ContentType; }
        if (FileUpload1.PostedFile.ContentLength > 0) { storeimage.Parameters.Add("@imagesize", SqlDbType.BigInt, 99999).Value = FileUpload1.PostedFile.ContentLength; }
        storeimage.Parameters.Add("@category", SqlDbType.NVarChar, 50).Value = lblSelection.Text;
        storeimage.Parameters.Add("@name", SqlDbType.NVarChar, 100).Value = TextBox2.Text;
        storeimage.Parameters.Add("@description", SqlDbType.NVarChar, 250).Value = TextBox3.Text;
        if (!string.IsNullOrEmpty(TextBox4.Text)) { storeimage.Parameters.Add("@username", SqlDbType.NVarChar, 50).Value = TextBox4.Text; }
        if (myimage != null) { storeimage.Parameters.Add("@userpicture", SqlDbType.VarBinary, myimage.Length).Value = myimage; }
        if (!string.IsNullOrEmpty(FileUpload2.PostedFile.ContentType)) { storeimage.Parameters.Add("@userpicturetype", SqlDbType.VarChar, 100).Value = FileUpload2.PostedFile.ContentType; }
        if (FileUpload2.PostedFile.ContentLength > 0) { storeimage.Parameters.Add("@userpicturesize", SqlDbType.BigInt, 99999).Value = FileUpload2.PostedFile.ContentLength; }

        try
        {
            myConnection.Open();
            storeimage.ExecuteNonQuery();
        }
        finally
        {
            myConnection.Close();
        }    
    }
}
于 2013-05-23T00:13:04.027 回答
0

@Jason 是对的,存储过程可以帮助分离 C# 和 SQL 代码,从而更容易调试。您还应该考虑使用SQL Profiler来“窥探”实际从 C# 发送到 SQL Server 的 SQL 语句。通过这种方式,您也许可以更清楚地看到 SQL 中的缺陷,然后再回到 C# 中进行修复。使用 SQL Profiler 时,我建议更改为“Tuning”模板以限制您正在查看的数据;创建新跟踪时应该很容易找到该设置。

于 2013-05-23T02:15:04.830 回答