我正在尝试使用更新查询将文件路径插入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");
}
}
答案 0 :(得分: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);