0

我在 .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;
       }
  1. 最后在业务层

    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;

            }
        }
4

1 回答 1

0

只需将其作为分号分隔的字符串存储在数据库中即可。

通过单选按钮构建字符串循环时,然后添加 ; 然后将完整的字符串存储在数据库中。

然后在重新填充数据时,只需将字符串拆分为 ; 并用每个项目填充数组/列表。

于 2012-09-19T09:24:31.933 回答