0

我的代码需要一些帮助。如何让 scope_Identiy 将 ImageId 存储为会话变量。我已经坚持了很长时间,但我无法让它发挥作用。

编辑:我相信从 SqlDataReader dataReader = cmd.ExecuteReader(); 下来是错误的。我只是不知道如何解决它。我对此还是陌生的。

protected void btnAddImage_Click(object sender, EventArgs e)
{

    //store items in session variables
    Session["AnimalName"] = AnimalNameTextBox.Text;          
    Session["TypeOfAnimal"] = TypeofAnimalDDL.Text;
    Session["Breed"] = BreedTextBox.Text;   
    Session["CrossBreed"] = CrossBreedAddDDL.Text;
    Session["Sex"] = SexDDL.Text;
    Session["Size"] = SizeDDL.Text;
    Session["Age"] = AgeDDL.Text;
    Session["Location"] = LocationAddDDL.Text;
    Session["Date"] = DateTextBox.Text;
    Session["Contact"] = ContactTextBox.Text;
    Session["Children"] = ChildrenDDL.Text;
    Session["OtherCats"] = OtherCatsDDL.Text;
    Session["OtherDogs"] = OtherDogsDDL.Text;
    Session["Neutered"] = NeuteredDDL.Text;
    Session["Microchipped"] = MicrochippedDDL.Text;
    Session["Colour"] = ColourTextBox.Text;
    Session["IndoorOutdoor"] = IndoorOutdoorDDL.Text;
    Session["Details"] = DetailsTextBox.Text;




      string s_Image_Name = txt_Image_Name.Text.ToString();
    if (FileUpload1.PostedFile != null && FileUpload1.PostedFile.FileName != "")
    {
        int newImageID ;
        byte[] n_Image_Size = new byte[FileUpload1.PostedFile.ContentLength];
        HttpPostedFile Posted_Image = FileUpload1.PostedFile;
        Posted_Image.InputStream.Read(n_Image_Size, 0, (int)FileUpload1.PostedFile.ContentLength);

        SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["RescueAnimalsIrelandConnectionString"].ConnectionString);

        SqlCommand cmd = new SqlCommand();
        cmd.CommandText = "INSERT INTO Images(Name,[Content],Size,Type) VALUES (@Image_Name,@Image_Content,@Image_Size,@Image_Type); SELECT ImageID = SCOPE_IDENTITY ()";
        cmd.CommandType = CommandType.Text;
        cmd.Connection = conn;


        SqlParameter Image_Name = new SqlParameter("@Image_Name", SqlDbType.VarChar, 500);
        Image_Name.Value = txt_Image_Name.Text;
        cmd.Parameters.Add(Image_Name);

        SqlParameter Image_Content = new SqlParameter("@Image_Content", SqlDbType.Image, n_Image_Size.Length);
        Image_Content.Value = n_Image_Size;
        cmd.Parameters.Add(Image_Content);

        SqlParameter Image_Size = new SqlParameter("@Image_Size", SqlDbType.BigInt, 99999);
        Image_Size.Value = FileUpload1.PostedFile.ContentLength;
        cmd.Parameters.Add(Image_Size);

        SqlParameter Image_Type = new SqlParameter("@Image_Type", SqlDbType.VarChar, 500);
        Image_Type.Value = FileUpload1.PostedFile.ContentType;
        cmd.Parameters.Add(Image_Type);


        SqlDataReader dataReader = cmd.ExecuteReader();

        if (dataReader.HasRows)
        {
            dataReader.Read();
            newImageID = Convert.ToInt32(dataReader["ImageID"]);
        }

        dataReader.Close();

        }
4

2 回答 2

1

两个建议:

首先,如果可能,请在存储过程中执行此操作。将您的新 id 作为输出参数返回

其次(这可能需要一些实验和拼写检查 - 我还没有尝试过),在您的命令中添加一个输出参数并稍微更改命令文本以获取值

SqlParameter Image_ID = new SqlParameter("@Image_ID", SqlDbType.Int);  
Image_ID.Direction = ParameterDirection.Output;
cmd.Parameters.Add(Image_ID); 

更改命令文本:SELECT @Image_ID = SCOPE_IDENTITY()

要读取结果:int newId = Convert.ToInt32(cmd.Parameters["@Image_ID"].Value)

于 2011-06-15T20:00:10.303 回答
0

尝试

Session["newImageID"] = Convert.ToInt32(dataReader["ImageID"]);

当你想检索 newImageId 时,使用

int newImageID = Session["newImageID"] as Int32;
于 2011-06-15T20:23:51.627 回答