使用scope_Identity获取ID以存储在会话变量中
问题描述:
我需要一些帮助来处理我的代码。我如何获得scope_Identiy来将ImageId存储为会话变量。我一直坚持这个多年,但我不能得到它的工作。使用scope_Identity获取ID以存储在会话变量中
编辑:我相信从行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();
}
答
两个建议:
首先,在存储过程中,如果有可能做到这一点。返回你的新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)
不解决这一问题,但你可以使用'ExecuteScalar',而不是使用DataReader – Earlz 2011-06-15 19:51:08
到底是什么错误/异常消息,你越来越多了?并且'newImageID'是一个类字段? – 2011-06-15 19:56:52
@bala没有错误,它只是不工作。我一直在努力工作了很长时间,并且让它保存图像,但不知道如何让它给我保存的行的ID。谢谢 – RyanS 2011-06-15 20:18:39