如何在C#上更新和显示数据库的结果

时间:2017-06-30 22:00:09

标签: c# mysql asp.net sql-server database

我正在尝试使用C#进行一些考试并创建学生系统。我对表行的可视化有一点问题。

我创建了文本框,我应该把学生的成绩放在哪里。我把这些值,但当我再次启动应用程序时,字段为空。当我打开桌子时 - 值就在那里。 另一个问题是,如果文本框中有成绩,我想更改它,我将不会在表中创建另一行,我想更新当前学生的行。表之间的通信是单元格“id”和“Faculty Number”。

程序:

CREATE PROCEDURE [dbo].InsertGrades
    @fakNum nvarchar(50),  
    @math int,
    @physic int,  
    @pik int,  
    @oip int,  
    @saa int,
    @ps int,  
    @pmu int,  
    @kp int,  
    @asls int,
    @pe int
AS
    INSERT INTO [Grades] (FakNumber, Math, Physic, PIK, OIP, SAA, PS, PMU, KP, ASLS, PE)
    VALUES (@fakNum , @math, @physic, @pik, @oip, @saa, @ps, @pmu, @kp, @asls, @pe)

Bool无效:

public static bool InsertGrades(Grade grd)
{
    GradesDataClassesDataContext grds = new GradesDataClassesDataContext();
    try
    {
        grds.InsertGrades(grd.FakNumber, grd.Math, grd.Physic, grd.PIK, grd.OIP, grd.SAA, grd.PS, grd.PMU, grd.KP, grd.ASLS, grd.PE);
        grds.SubmitChanges();
    }
    catch (Exception e)
    {
        System.Windows.Forms.MessageBox.Show(e.Message);
        return false;
    }
    return true;
}

另一个布尔:

public static bool InsertGrades(Grade g)
{
    if (g.Math > 6 && g.Math < 2 &&
        g.Physic > 6 && g.Physic < 2 &&
        g.PIK    > 6 && g.PIK    < 2 &&
        g.OIP    > 6 && g.OIP    < 2 &&
        g.SAA    > 6 && g.SAA    < 2 &&
        g.PS     > 6 && g.PS     < 2 &&
        g.PMU    > 6 && g.PMU    < 2 &&
        g.KP     > 6 && g.KP     < 2 &&
        g.ASLS   > 6 && g.ASLS   < 2 &&
        g.PE     > 6 && g.PE     < 2)

    {
        return UserData.InsertGrades(g);
    }
    else
    {
        return false;
    }
}

这必须将值从表格插入到文本框,但它不会

private Data.Grade ReadStudentGrades()
{
    Data.Grade grd = new Data.Grade();
    grd.FakNumber = txtFakNumber.Text;
    grd.Math = Int32.Parse(txtMath.Text);
    grd.Physic = Int32.Parse(txtPhysic.Text);
    grd.PIK = Int32.Parse(txtPIK.Text);
    grd.OIP = Int32.Parse(txtOIP.Text);
    grd.SAA = Int32.Parse(txtSAA.Text);
    grd.PS = Int32.Parse(txtPS.Text);
    grd.PMU = Int32.Parse(txtPMU.Text);
    grd.KP = Int32.Parse(txtKP.Text);
    grd.ASLS = Int32.Parse(txtASLS.Text);
    grd.PE = Int32.Parse(txtPE.Text);
    grd.FakNumber = txtFakNumber.Text;

    return grd;
}

1 个答案:

答案 0 :(得分:0)

关于此代码:

  

这必须从表格到文本框插入值,但它不会:

private Data.Grade ReadStudentGrades()
{
    Data.Grade grd = new Data.Grade();
    grd.FakNumber = txtFakNumber.Text;
    grd.Math = Int32.Parse(txtMath.Text);
    grd.Physic = Int32.Parse(txtPhysic.Text);
    grd.PIK = Int32.Parse(txtPIK.Text);
    grd.OIP = Int32.Parse(txtOIP.Text);
    grd.SAA = Int32.Parse(txtSAA.Text);
    grd.PS = Int32.Parse(txtPS.Text);
    grd.PMU = Int32.Parse(txtPMU.Text);
    grd.KP = Int32.Parse(txtKP.Text);
    grd.ASLS = Int32.Parse(txtASLS.Text);
    grd.PE = Int32.Parse(txtPE.Text);
    grd.FakNumber = txtFakNumber.Text;

    return grd;
}

此代码无法将值从表格插入到文本框中。这里没有一行可以为任何文本框分配值。

也许你倒退了?也许它应该更像这样:

private Data.Grade ReadStudentGrades()
{
    Data.Grade grd = new Data.Grade();
    txtFakNumber.Text = grd.FakNumber;
    txtMath.Text = grd.Math.ToString();
    txtPhysic.Text = grd.Physic.ToString();
    txtPIK.Text = grd.PIK.ToString();
    (txtOIP.Text = grd.OIP.ToString();
    txtSAA.Text = grd.SAA.ToString();
    txtPS.Text = grd.PS.ToString();
    txtPMP.Text = grd.PMU.ToString();
    txtKP.Text = grd.KP.ToString();
    txtASLA.Text = grd.ASLS.ToString();
    txtPE.Text = grd.PE.ToString();
    txtFakNumber.Text = grd.FakNumber;

}