///
///添加,编辑问卷///
///
///
public intAddQuestionnaire(Entity.Questionnaire.Questionnaire questionnaire)
{int identity = 0;
SqlConnection connection= new SqlConnection(ConfigurationManager.ConnectionStrings["SqlConnectionStringTrainingOA"].ConnectionString);
SqlCommand command=connection.CreateCommand();
SqlTransaction transaction;
connection.Open();
transaction=connection.BeginTransaction();
command.Transaction=transaction;
command.CommandType=CommandType.StoredProcedure;try{//1写入问卷
command.CommandText = "Questionnaire_ADD";
SqlParameter[] parms= newSqlParameter[] {new SqlParameter("@ID",SqlDbType.Int),new SqlParameter("@QuestionnaireTitle",SqlDbType.NVarChar)
};
parms[0].Value =questionnaire.ID;
parms[1].Value =questionnaire.QuestionnaireTitle;
command.Parameters.AddRange(parms);int questionnaireID =Convert.ToInt32(command.ExecuteScalar());
identity=questionnaireID;//2写入题目
for(int i = 0; i < questionnaire.Questions.Count; i++)
{
command.CommandText= "Question_ADD";
command.Parameters.Clear();
parms= newSqlParameter[] {new SqlParameter("@ID",questionnaire.ID),new SqlParameter("@QuestionTitle",questionnaire.Questions[i].QuestionTitle),new SqlParameter("@Sequence",questionnaire.Questions[i].Sequence),new SqlParameter("@QuestionTypeID",questionnaire.Questions[i].QuestionTypeID),new SqlParameter("@QuestionnaireID",questionnaireID)};
command.Parameters.AddRange(parms);int questionID =Convert.ToInt32(command.ExecuteScalar());
identity=questionID;if((questionnaire.Questions[i].QuestionType == QuestionType.singlechoice) || (questionnaire.Questions[i].QuestionType ==QuestionType.multiplechoice))
{for(int j = 0; j < questionnaire.Questions[i].ChoiceItems.Count; j++)
{
command.CommandText= "ChoiceItem_ADD";
command.Parameters.Clear();
parms= newSqlParameter[]{new SqlParameter("@ID",questionnaire.Questions[i].ChoiceItems[j].ID),new SqlParameter("@ItemContent",questionnaire.Questions[i].ChoiceItems[j].ItemContent),new SqlParameter("@Options",questionnaire.Questions[i].ChoiceItems[j].Option),new SqlParameter("@QuestionID",questionID)};
command.Parameters.AddRange(parms);
identity=Convert.ToInt32(command.ExecuteScalar());
}
}
}
transaction.Commit();
}catch(Exception ex)
{
transaction.Rollback();throw;
}finally{
connection.Close();
transaction.Dispose();
connection.Dispose();
}returnidentity;
}