我在 .aspx 页面(在 asp.net 中)中有 10 个单选按钮列表,在数据库中的一行(名称答案)
1.在模型层,我提到了这段代码
public class DimensionQuestion
{
public string NewCompanyName { get; set; }
public string NewSurveyName { get; set; }
public List<int> NewAnswer { get; set; }
}
2.在数据层层,我提到了这段代码,
public static bool InsertNewDimAnswer(DimensionQuestion dimension)
{
bool result;
using (var helper = new DbHelper())
{
_cmdtext = "sp_InsertNewDimAnswer";
var success = new SqlParameter("@Success", SqlDbType.Bit, 1, ParameterDirection.Output, true, 0, 0,
"Result", DataRowVersion.Default, 0);
foreach (string s in dimension.NewAnswer)
{
if (s.Trim().Length > 0)
{
var parameter = new[]
{
new SqlParameter("@CompanyName", dimension.NewCompanyName),
new SqlParameter("@SurveyName", dimension.NewSurveyName),
new SqlParameter("@Answer",s ),
success,
};
helper.ExecuteScalar(_cmdtext, CommandType.StoredProcedure, parameter);
}
}
result = (bool)success.Value;
}
return result;
}
最后在业务层
private void FillObjects() { List Answer = new List(); Answer.Add(Convert.ToInt32(rbAnswer1.Text.Trim())); Answer.Add(Convert.ToInt32(rbAnswer2.Text.Trim())); Answer.Add(Convert.ToInt32(rbAnswer3.Text.Trim())); Answer.Add(Convert.ToInt32(rbAnswer4.Text.Trim())); Answer.Add(Convert.ToInt32(rbAnswer5.Text.Trim())); Answer.Add(Convert.ToInt32(rbAnswer6.Text.Trim())); Answer.Add(Convert.ToInt32(rbAnswer7.Text.Trim())); Answer.Add(Convert.ToInt32(rbAnswer8.Text.Trim())); Answer.Add(Convert.ToInt32(rbAnswer9.Text.Trim())); Answer.Add(Convert.ToInt32(rbAnswer10.Text.Trim())); Answer.Add(Convert.ToInt32(rbAnswer11.Text.Trim())); Answer.Add(Convert.ToInt32(rbAnswer12.Text.Trim())); 回答。添加(Convert.ToInt32(rbAnswer13.Text.Trim())); Answer.Add(Convert.ToInt32(rbAnswer14.Text.Trim())); Answer.Add(Convert.ToInt32(rbAnswer15.Text.Trim())); _DimensionQuestion.NewAnswer = 答案;}
并在按钮上单击
protected void btnSave_Click(object sender, EventArgs e)
{
try
{
FillObjects();
if (InsertData.InsertNewDimAnswer(_DimensionQuestion)
{
ShowMessage("Information is saved");
Reset();
}
else
{
ShowMessage("Please try again");
}
}
finally
{
//_DimensionQuestion = null;
}
}