保存从sql数据库读取的数据

时间:2017-07-25 14:54:30

标签: c#-4.0

我有四个文本框和一个metrogrid,在文本框中,数据是从sql数据库中读入的,但是在文本框中保存数据时,一个会保存,而另一个则会消失。我需要帮助 这是搜索按钮的代码:

private void btnSearch_Click(object sender, EventArgs e)
{
    try 
    {
        using (SqlConnection connection = new SqlConnection(@"server = .\sql2012; database = dbAcceptance; integrated security = true; "))
        {
            SqlCommand command = new SqlCommand($"select * from acceptance WHERE regno = {txtregno.Text}", connection);

            connection.Open();

            SqlDataReader read = command.ExecuteReader();

            if (read.Read())
            {
                txtfullname.Text = (read["fullname"].ToString());
                txtdepartment.Text = (read["Department"].ToString());
                txtfaculty.Text = (read["faculty"].ToString());
                txtmodeofstudy.Text = (read["modeofstudy"].ToString());
                txtprogramme.Text = (read["programmeofstudy"].ToString());
            }
            else
                MetroFramework.MetroMessageBox.Show(this, "Reg No Not Found...Please Try Again", "Message");
            read.Close();
        }
    }
    catch (Exception ex)
    {
        //MetroFramework.MetroMessageBox.Show(this, ex.Message, "Message", MessageBoxButtons.OK, MessageBoxIcon.Error);
    }  
} 

这是添加按钮的代码:

private void btnAdd_Click(object sender, EventArgs e)
{
        objState = EntityState.Added;

        pContainer.Enabled = true;

        registeredBindingSource.Add(new Registered());

        registeredBindingSource.MoveLast();

        txtregno.Focus();
}

这是保存按钮的代码:

private void btnSave_Click(object sender, EventArgs e)
{
        try
        {
            registeredBindingSource.EndEdit();
            Registered obj = registeredBindingSource.Current as Registered;

            if (obj != null)
            {
                using (IDbConnection db = new SqlConnection(ConfigurationManager.ConnectionStrings["conn"].ConnectionString))
                {
                    if (db.State == ConnectionState.Closed)
                        db.Open();
                    if (objState == EntityState.Added)
                    {
                        DynamicParameters p = new DynamicParameters();
                        p.Add("@StudentID", dbType: DbType.Int32, direction: ParameterDirection.Output);
                        p.AddDynamicParams(new { 
                                                FullName = txtfullname.Text,
                                                RegNo = txtregno.Text,
                                                CourseTitle = obj.CourseTitle,
                                                CourseStatus = obj.CourseStatus, 
                                                CourseUnit = obj.CourseUnit,
                                                Department = txtdepartment.Text,
                                                CurrentSemester = obj.CurrentSemester, 
                                                CurrentSession = obj.CurrentSession,
                                                ModeOfStudy = txtmodeofstudy.Text, 
                                                Level = obj.Level, 
                                                ProgrammeOfStudy = txtprogramme.Text, 
                                                Faculty = txtfaculty.Text, finalSemester = obj.finalSemester
                                               });
                        db.Execute("sp_StudentRegisteredCourse_insert", p, commandType: CommandType.StoredProcedure);
                        obj.StudentID = p.Get<int>("@StudentID");
                    }
                    else
                    {
                        db.Execute("sp_StudentRegisteredCourse_update", new { StudentID = obj.StudentID,FullName = txtfullname.Text,RegNo = txtregno.Text, CourseTitle = obj.CourseTitle,CourseStatus = obj.CourseStatus, CourseUnit = obj.CourseUnit, Department = txtdepartment.Text, CurrentSemester = obj.CurrentSemester,CurrentSession = obj.CurrentSession, ModeOfStudy = txtmodeofstudy.Text, Level = obj.Level, ProgrammeOfStudy = txtprogramme.Text,Faculty = txtfaculty.Text, finalSemester = obj.finalSemester },commandType:CommandType.StoredProcedure);
                    }
                    gridContainer.Refresh();
                    pContainer.Enabled = false;
                    objState = EntityState.unchanged;
                }
            }
        }
        catch (Exception ex)
        {
            MetroFramework.MetroMessageBox.Show(this, ex.Message, "Message", MessageBoxButtons.OK, MessageBoxIcon.Error);
        }
}

0 个答案:

没有答案