图像名称已存在,它会更新数据 [英] image name is already exists it update the data

查看:98
本文介绍了图像名称已存在,它会更新数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将图像名称和图像路径存储在database.i想要当图像名称已经存在时更新数据但是有一些问题。我这个代码更新图像但是给出错误ExecuteNonQuery:Connection属性还没有初始化。

实际上我想要的如果图像名称存在则更新它,如果没有插入它???







i am storing image name and image path in database.i want when image name is already exists it update the data but there is some problem.my this code it update the image but gives error "ExecuteNonQuery: Connection property has not been initialized."
actually i want if image name exists update it if not insert it???



protected void Button2_Click1(object sender, EventArgs e)
        {
            {
                string username =(string)( Session["UserAuthentication"]);
                //Get Filename from fileupload control
               // string filename = Path.GetFileName(fileuploadimages.PostedFile.FileName);
                //Save images into Images folder
                FileUpload1.SaveAs(Server.MapPath("Images/" + username));
                string str = "Images/" + username;
                //Getting dbconnection from web.config connectionstring
                SqlConnection con = new SqlConnection(connstring);
                //Open the database connection
                con.Open();
                //Query to insert images path and name into database

                //SqlCommand cmd = new SqlCommand("Insert into imagePath(imagename,imgpath) values(@imagename,@imgpath)", con);
                cmd=new SqlCommand("UPDATE imagepath SET imgpath='"+str+"' WHERE imagename='"+username+"'");
                //Passing parameters to query
               cmd.Parameters.AddWithValue(username, username);
               cmd.Parameters.AddWithValue(str, "Images/" + username);
                cmd.ExecuteNonQuery();
                //Close dbconnection
                con.Close();
                //Response.Redirect("~/Default.aspx");
            }

推荐答案

SqlCommand 中添加连接对象作为第二个参数。试试这个:

Add connection object in SqlCommand as second parameter. Try this:
cmd=new SqlCommand("UPDATE imagepath SET imgpath='"+str+"' WHERE imagename='"+username+"'", con);







首先,您需要检查数据库中该图像的计数。如果图像存在于数据库中,则计数将大于零(在本例中为1)。如果计数大于零,则更新图像,否则插入。

试试这个:




First you need to check the count in database for that image. If image exists in database the count will be more than zero(in this case 1). If count is coming more than zero then update the image otherwise insert that.
Try this:

cmd=new SqlCommand("SELECT COUNT(*) FROM imagepath  WHERE imagename='"+username+"'", con);
int count  = Convert.ToInt32(cmd.ExecuteScalar());
if(count > 0){
    //Write update code here.
}
else{
    //Write insert code here.
}







- Amit


protected void Button2_Click1(object sender, EventArgs e)
        {
            {
                string username =(string)( Session["UserAuthentication"]);
                FileUpload1.SaveAs(Server.MapPath("Images/" + username));
                string str = "Images/" + username;
                SqlConnection con = new SqlConnection(connstring);
                con.Open();
                cmd=new SqlCommand("UPDATE imagepath SET imgpath='"+str+"' WHERE imagename='"+username+"'", con);//You need to mention connection string object here with command                
               cmd.Parameters.AddWithValue(username, username);
               cmd.Parameters.AddWithValue(str, "Images/" + username);
                cmd.ExecuteNonQuery();               
                con.Close();
            }


Hi ,
define the connection in the query ending   "CON"
replace the below line i8n you project

cmd=new SqlCommand("UPDATE imagepath SET imgpath='"+str+"' WHERE imagename='"+username+"'",con);


Thankyou


这篇关于图像名称已存在,它会更新数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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