假设 cardDetailsID 为 5。
查看数据库的cardDetails表中的第5条记录,可以看到它的其他字段包括“bgcolour”和“borderstyle”。因此,对于这个特定的记录,我有 cardDetailsID = 5、bgcolour = blue、bordersytle=solid。
我希望能够从cardDetailsID 中获取bgcolour 和bordersyle 设置(蓝色和纯色)。
这是到目前为止的代码。查询字符串中的值正在工作(正在传递数字“5”)但现在我如何获得该行的其余设置?
cardDetailsIDrv.Text = Request.QueryString["cardDetailsID"];
cardDetailsIDrv.Visible = false;
//create Connection
SqlConnection myConnection = new SqlConnection(ConfigurationManager.AppSettings["ConnString"]);
//create Command
SqlCommand getCardDetailsCMD = new SqlCommand("SELECT * FROM cardDetails WHERE cardDetailsID =" + Convert.ToInt32(cardDetailsIDrv.Text), myConnection);
myConnection.Open();
//create datareader
SqlDataReader myReader = getCardDetailsCMD.ExecuteReader();
//code works properly up till here
try
{
//Using DataReader to retrieve info from the database and display it on the panel
while (myReader.Read())
{
//I'm guessing here is where I'm messing things up
pnlCardps.BackColor = Color.FromName(myReader["bgcolour"].ToString());
pnlCardps.BorderStyle = (BorderStyle)Enum.Parse(typeof(BorderStyle), myReader["borderstyle"].ToString());
}
}
finally
{
if (myReader != null)
{
myReader.Close();
}
if (myConnection != null)
{
myConnection.Close();
}
}
问题解决了!!我所要做的就是将 while 循环中的代码调整为:
string bgColour = myReader["bgColour"].ToString();
pnlCardrv.BackColor = Color.FromName(bgColour);
string borderColour = myReader["borderColour"].ToString();
pnlCardrv.BorderColor = Color.FromName(borderColour);