如何在c#中使用HiddenField id作为其值的变量



我有1000个隐藏字段。我怎么能把它们的值在sql数据库使用循环。如:

for (int i = 1; i < 1000; i++)
{
Control hiddenfield = this.FindControl("HiddenField" + i);

String p = Convert.ToString(hiddenfield.Value);
string sqlquery = ("INSERT INTO [" + table_name2 + "] (CT1) VALUES ('" + p + "')");
SqlCommand command = new SqlCommand(sqlquery, Connection);
command.ExecuteNonQuery();
}

根据需要更改表名!

string query = "INSERT INTO tablename ( CT1 ) VALUES ( @value )";
SqlConnection con = new SqlConnection(constr);
SqlCommand cmd = new SqlCommand(query, con);
try
{  
cmd.Parameters.Add("@value", System.Data.SqlDbType.VarChar);
con.Open();
for (int i = 1; i < 1000; i++)
{
Control hiddenfield = this.FindControl("HiddenField" + i);        
String p = Convert.ToString(hiddenfield.Value);
cmd.Parameters["@value"].Value = p;
cmd.ExecuteNonQuery();
}
}
catch (Exception ex)
{
//Show exception as required!
}
finally
{
con.Close();
con.Dispose();
cmd.Dispose();
}

最新更新