现在又一列列名,分别为zb1,zb2...zb107.想给每一列存进dropdownlist里的value值。zb后边的变量1-107想用一个循环的变量来控制
for (int i = 1; i <= 107; i++)
{
ContentPlaceHolder cph = this.Form.FindControl("ContentPlaceHolder2") as ContentPlaceHolder;
DropDownList ddl = cph.FindControl("DropDownList" + i) as DropDownList; if (ddl != null)
{
int a = int.Parse(ddl.SelectedValue);
int b = Convert.ToInt32(TextBox1.Text);
SqlConnection con = new SqlConnection("Server=.;database=QINXINJITUAN;uid=sa;pwd=179425532;");
try
{
con.Open();
string str = "update ZHtjqrx set zb='" + a+ "'where year='" + b + "'";
SqlCommand cmd = new SqlCommand(string.Format(str), con);
cmd.ExecuteNonQuery();
}
catch (Exception ex)
{
Response.Write(ex.Message);
}
finally
{ con.Close();
}
} }
关键行为红色的那一行,请问zb那一块怎么写?
for (int i = 1; i <= 107; i++)
{
ContentPlaceHolder cph = this.Form.FindControl("ContentPlaceHolder2") as ContentPlaceHolder;
DropDownList ddl = cph.FindControl("DropDownList" + i) as DropDownList; if (ddl != null)
{
int a = int.Parse(ddl.SelectedValue);
int b = Convert.ToInt32(TextBox1.Text);
SqlConnection con = new SqlConnection("Server=.;database=QINXINJITUAN;uid=sa;pwd=179425532;");
try
{
con.Open();
string str = "update ZHtjqrx set zb='" + a+ "'where year='" + b + "'";
SqlCommand cmd = new SqlCommand(string.Format(str), con);
cmd.ExecuteNonQuery();
}
catch (Exception ex)
{
Response.Write(ex.Message);
}
finally
{ con.Close();
}
} }
关键行为红色的那一行,请问zb那一块怎么写?
" + a+ " where year=" + b + "
上面是int
下面是string 你的sql语句
多加一个参数就好啦