DataBase插入代码背后

时间:2013-05-22 22:36:53

标签: c# .net sql-server

尝试在文本和图像中插入数据库。唯一必填字段是“类别”,“名称”和“说明”。如果只有那些字段有条目,则插入数据库不成功。如果加载了不需要的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;
        }
    }
}

2 个答案:

答案 0 :(得分:0)

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

我建议你(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 方法,请注意我已将执行包含在aa try / finally中,以确保连接已关闭,您也可以使用{{3如果您愿意,请填写

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();
        }    
    }
}

答案 1 :(得分:0)

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