2

我正在尝试使用更新查询将文件路径插入 SQL Server。但是,尽管查询成功运行且没有错误,但我无法看到更改。这是我的 asp.net 网络应用程序的代码。我试图在我的数据库中的表列中插入流值,在本地机器上执行此操作,

protected void Uplad(object sender, EventArgs e)
    {
        string phone = Session["phn"].ToString();
        string base64 = Request.Form["imgCropped"];
        byte[] bytes = Convert.FromBase64String(base64.Split(',')[1]);
        using (FileStream stream = new FileStream(Server.MapPath("~/Images/dp/" + phone + ".png"), FileMode.Create))
        {
            str = stream.ToString(); 
            con.Open();
            SqlCommand dpcmd = new SqlCommand("update xyz set ab = '" + str + "' where Moile ='" + phone + "'", cnn); //if i mouseover i can see the value of stream as C://xyz.png (cant write full path here) but when i check in db it is not showing up...
            stream.Write(bytes, 0, bytes.Length);
            stream.Flush();
            con.Close();
        }

        display.Visible = false;
    }

我也可以在编译时看到值,文件也被保存到指定的文件夹,但是为什么数据库没有更新?它只取旧值吗?如何在 isPostback 中相应地实施更改?先感谢您 :)

protected void Page_Load(object sender, EventArgs e)
    {


        try
        {

            if (!IsPostBack) // this block doesnt get executed when above handler is executed...hence i am assigning a value in the else to test
            {
                string phone = Convert.ToString(Session["pn"]);
                oldbi.Text = phone; //old number 
                usrname.Text = Convert.ToString(Session["fname"]); //assigning the name
                nm = Convert.ToString(Session["paw"]);
                if (phone != "")
                {
                    SetInitialRow();
                }
                else
                {
                    Response.Redirect("join.aspx");
                }

            }
            else
            {
                str = "do"; // i tried to assign a value here and i tried inserting this value in update statement but even this is not showing up in DB. I think it is not updating new value
            }
        }
        catch
        {
            Response.Redirect("join.aspx");
        }
    }
4

1 回答 1

2

您还需要放置以下行。您所做的只是创建了一个SqlCommand,但您还没有针对connection.

dpcmd.CommandType=CommandType.Text;
dpcmd.ExecuteNonQuery();

更新

如果您只想保存文件名,那么这可能会有所帮助

string str=phone + ".png";
SqlCommand dpcmd = new SqlCommand("update xyz set ab = '" + str + "' where Mobile ='" + phone + "'", cnn);
于 2016-02-19T10:50:43.450 回答