【发布时间】:2011-07-19 22:39:48
【问题描述】:
我有一个字符串:
string theUserId = Session["UserID"].ToString();
但我不知道如何将字符串添加到这个 sqlsnytax
{
if (Session["UserID"] != null)
{
string theUserId = Session["UserID"].ToString();
Label1.Text = Convert.ToString(theUserId);
OdbcConnection cn = new OdbcConnection("Driver={MySQL ODBC 3.51 Driver}; Server=localhost; Database=gymwebsite; User=x; Password=x;");
cn.Open();
OdbcCommand cmd = new OdbcCommand("SELECT User.FirstName, User.SecondName, User.Aboutme, User.DOB, Pictures.picturepath FROM User LEFT JOIN Pictures ON User.UserID = Pictures.UserID WHERE User.UserID=@UserID"), cn);
cmd.Parameters.AddWithValue("@UserID", theUserId);
OdbcDataReader reader = cmd.ExecuteReader();
while (reader.Read())
{
Name.Text = String.Format("{0} {1}", reader.GetString(0), reader.GetString(1));
Aboutme.Text = String.Format("{0}", reader.GetString(2));
Age.Text = String.Format("{0}", reader.GetString(3));
Image1.ImageUrl = String.Format("{0}", reader.GetString(4));
}
}
}
}
User.UserID=1 如何将其更改为 User.UserID="theUserId"
【问题讨论】:
-
@Brandon 根据要求给出了正确答案;但是,有几点需要注意。首先,“theUserId”需要在执行前进行清理。就目前而言,这上面写满了 SQL 注入。其次,我不确定你也传递这个是什么,但看起来确实有人需要把它撕掉。您可能想在 codereview.stackexchange.com 上发布一些代码
-
@ukhardy,已删除。 WraithNath 基本上有相同的答案,没有所有的噪音。我删除了我的并赞成他的。
-
谢谢布兰登,我会效仿的。