如何使用ASP.NET C#中的实体框架和存储过程更新数据库中的记录 [英] How to update record in database using entity framework and stored procedure in ASP.NET C#

查看:81
本文介绍了如何使用ASP.NET C#中的实体框架和存储过程更新数据库中的记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

存储过程代码:

Stored Procedure code:

ALTER PROCEDURE [dbo].[imgupload]
@ProfilePictureName nvarchar(255),
@ProfilePicture varbinary(MAX),
@ProfilePicSize int,
@NewId int
as
begin
update tblUserProfile set ProfilePicture=@ProfilePicture, ProfilePictureName=@ProfilePictureName,ProfilePicSize=@ProfilePicSize where UserSignUpId=@NewId
select 1
end





Page_load:



Page_load :

if(Request.QueryString["Mode"]=="Updt")
                   {
                       using (dbEntities sc = new dbEntities ())
                       {
                           Int64 userid = Int64.Parse(new StandardModule().Decrypt(HttpUtility.UrlDecode(Request.QueryString["uid"])).ToString());
                           GetUserDetails(Int64.Parse(new StandardModule().Decrypt(HttpUtility.UrlDecode(Request.QueryString["uid"])).ToString()));
                           var query = sc.tblUserProfiles.FirstOrDefault(d => d.UserSignUpId == userid);
                           query.Email = email;
                           query.ProfileName = profilename;
                           query.UserName = username;
                           query.BioData = bio;
                           sc.SaveChanges();
                       }
                   }





Update_Profile图片:



Update_Profile Picture:

<pre> protected void btnUpdateProfile_Click(object sender, EventArgs e)
    {
        HttpPostedFile postedfile = FileUpload1.PostedFile;
        string fileName = Path.GetFileName(postedfile.FileName);
        string fileExtension = Path.GetExtension(fileName);
        int fileSize = postedfile.ContentLength;

        if (fileExtension.ToLower() == ".jpg")
        {
          
            Stream stream = postedfile.InputStream;
            BinaryReader binaryReader = new BinaryReader(stream);
            byte[] bytes = binaryReader.ReadBytes((int)stream.Length);
            string cs = ConfigurationManager.ConnectionStrings["cnstr"].ConnectionString;
            using (SqlConnection con = new SqlConnection(cs))
            {
               SqlCommand cmd = new SqlCommand("imgupload", con);           
                
                cmd.CommandType = CommandType.StoredProcedure;

                SqlParameter paramName = new SqlParameter()
                {
                    ParameterName = "@ProfilePictureName",
                    Value = fileName
                };
                cmd.Parameters.Add(paramName);

                SqlParameter paramSize = new SqlParameter()
                {
                    ParameterName = "@ProfilePicSize",
                    Value = fileSize
                };
                cmd.Parameters.Add(paramSize);

                SqlParameter paramImage = new SqlParameter()
                {
                    ParameterName = "@ProfilePicture",
                    Value = bytes
                };

                cmd.Parameters.Add(paramImage);

                

                SqlParameter paramNewId = new SqlParameter()
                {
                    ParameterName = "@NewId",
                    Value = Int64.Parse(Request.QueryString["uid"])

                };
                cmd.Parameters.Add(paramNewId);

                con.Open();
                cmd.ExecuteNonQuery();
                con.Close();
                string uid = HttpUtility.UrlEncode(new StandardModule().Encrypt(Session["userid"].ToString()));
                Response.Redirect(string.Format("~/User/UserProfile.aspx?Mode=Updt&uid={0}",uid),false);


            }
        }
    }





什么我试过了:



问题不是在用户的特定ID中插入数据库而不是图像插入新行。



请帮助我。



错误排除



What I have tried:

The Problem is not inserting into database at the particular Id of the user instead of it the image insert into new row.

Please Help me.

The error is in line

SqlParameter paramNewId = new SqlParameter()
               {
                   ParameterName = "@NewId",
                   Value = Int64.Parse(Request.QueryString["uid"])

               };



即输入字符串的格式不正确。


ie input string was not in the correct format.

推荐答案

你好,

错误消息表明您正在尝试将提供的值转换为Integer,这绝对不是有效的整数。那么,检查来自QueryString的值是什么。

设置一个断点并进行调试。

谢谢
Hello,
Error message says that you are trying to convert the provided value into Integer which is definitely not a valid integer. so, check what is the value coming from QueryString.
Put a break point and debug it.
Thanks


我建​​议你创建完整的TSQL字符串并执行它们,而不是使用各种模式等功能。



UPDATE [table] SET .... WHERE .... < b / b


这样做更好,不仅因为可读性,而且因为现在可以根据需要在语言之间轻松传输您的部分工作 - 并且可以直接在工具中测试,例如作为SQL Server Mgmt Studio,以确保您的查询符合您的要求。



上述更新,例如,永远不会插入新记录
I suggest you create full TSQL string and execute those, rather than use the functionality with various modes and such.

UPDATE [table] SET .... WHERE....

This is better not only because of readability but because that part of your work in now readily transported between languages should the need arise - and can be tested directly in tools, such as SQL Server Mgmt Studio, to make sure your query does what you want it to.

The above "UPDATE", for example, would never INSERT a new record


解决:

Solved:
Int64 userid = Int64.Parse(new StandardModule().Decrypt(HttpUtility.UrlDecode(Request.QueryString["uid"])).ToString());





i刚解密即将到来的id从登录页面然后我像这样使用它







i just decrypt the id which is coming from login page then i use it like this


SqlParameter paramNewId = new SqlParameter()
                {
                    ParameterName = "@NewId",
                    Value = userid
 
                };





并且工作正常。



谢谢。



and it works fine.

Thanks.


这篇关于如何使用ASP.NET C#中的实体框架和存储过程更新数据库中的记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆