如何使用Entity Framework将文本框中的数据保存到数据库?

huangapple go评论168阅读模式
英文:

How can save date from textbox to database using Entity Framework?

问题

dtpGraduationYear 是一个包含日期的 TextBox
我想要使用 Entity Framework 将 textbox 中的日期保存到数据库中。

private void btnSaveAll_Click(object sender, EventArgs e)
{
    //
    // 教育选项卡
    //         
    var Education = new database.tblEmployeeQualification
    {
        EmployeeCode = Convert.ToInt32(txtCode.Text),
        UniversitiyCode = string.IsNullOrEmpty(txtEdUniversitiesNo.Text) ? (byte?)null : Convert.ToByte(txtEdUniversitiesNo.Text), 
        FauculityCode = string.IsNullOrEmpty(txtEdFacultyNo.Text) ? (byte?)null : Convert.ToByte(txtEdFacultyNo.Text), 
        QualificationsCode = string.IsNullOrEmpty(txtEdQualificationNo.Text) ? (byte?)null : Convert.ToByte(txtEdQualificationNo.Text), 
        SpecializationCode = string.IsNullOrEmpty(txtEdSpecializationNo.Text) ? (byte?)null : Convert.ToByte(txtEdSpecializationNo.Text), 
        EductionGrade = string.IsNullOrEmpty(txtEdGradeNo.Text) ? (byte?)null : Convert.ToByte(txtEdGradeNo.Text), 
        QualificationsTypeCode = string.IsNullOrEmpty(txtQualificationTypeNo.Text) ? (byte?)null : Convert.ToByte(txtQualificationTypeNo.Text), 
        GraduationYear = dtpGraduationYear.ToString("dd/MM/yyyy"),
        Note = txtEdNote.Text,
    };

    db.tblEmployeeQualifications.AddOrUpdate(Education);
    db.SaveChanges();
}

错误在这里
GraduationYear = dtpGraduationYear.ToString("dd/MM/yyyy"),
错误:

> 方法 'tostring' 没有接受 1 个参数的重载

这是我的模型类:

public partial class tblEmployeeQualification
{
    public int EmployeeCode { get; set; }
    public Nullable<short> UniversitiyCode { get; set; }
    public Nullable<short> FauculityCode { get; set; }
    public Nullable<short> QualificationsCode { get; set; }
    public Nullable<short> SpecializationCode { get; set; }
    public Nullable<byte> EductionGrade { get; set; }
    public Nullable<byte> QualificationsTypeCode { get; set; }
    public Nullable<byte> MainEducationCode { get; set; }
    public Nullable<System.DateTime> GraduationYear { get; set; }
    public string Note { get; set; }
    
    public virtual tblEmployeeData tblEmployeeData { get; set; }
    public virtual tblEnglishLevel tblEnglishLevel { get; set; }
    public virtual tblFaculty tblFaculty { get; set; }
    public virtual tblMainEduaction tblMainEduaction { get; set; }
    public virtual tblQualification tblQualification { get; set; }
    public virtual tblQualificationType tblQualificationType { get; set; }
    public virtual tblSpecialization tblSpecialization { get; set; }
    public virtual tblUniversity tblUniversity { get; set; }
}
英文:

dtpGraduationYear is a TextBox that contains a date.
I want to save the date from the textbox to database using Entity Framework.

private void btnSaveAll_Click(object sender, EventArgs e)
{
    //
    // tab Education
    //         
    var Education = new database.tblEmployeeQualification
    {
        EmployeeCode = Convert.ToInt32(txtCode.Text),
        UniversitiyCode = string.IsNullOrEmpty(txtEdUniversitiesNo.Text) ? (byte?)null : Convert.ToByte(txtEdUniversitiesNo.Text), 
        FauculityCode = string.IsNullOrEmpty(txtEdFacultyNo.Text) ? (byte?)null : Convert.ToByte(txtEdFacultyNo.Text), 
        QualificationsCode = string.IsNullOrEmpty(txtEdQualificationNo.Text) ? (byte?)null : Convert.ToByte(txtEdQualificationNo.Text), 
        SpecializationCode = string.IsNullOrEmpty(txtEdSpecializationNo.Text) ? (byte?)null : Convert.ToByte(txtEdSpecializationNo.Text), 
        EductionGrade = string.IsNullOrEmpty(txtEdGradeNo.Text) ? (byte?)null : Convert.ToByte(txtEdGradeNo.Text), 
        QualificationsTypeCode = string.IsNullOrEmpty(txtQualificationTypeNo.Text) ? (byte?)null : Convert.ToByte(txtQualificationTypeNo.Text), 
        GraduationYear = dtpGraduationYear.ToString(&quot;dd/MM/yyyy&quot;),
        Note = txtEdNote.Text,
    };

    db.tblEmployeeQualifications.AddOrUpdate(Education);
    db.SaveChanges();
}

Error is here
GraduationYear = dtpGraduationYear.ToString(&quot;dd/MM/yyyy&quot;),
error:

> no overload for method 'tostring' takes 1 arguments

This is my model class:

public partial class tblEmployeeQualification
{
    public int EmployeeCode { get; set; }
    public Nullable&lt;short&gt; UniversitiyCode { get; set; }
    public Nullable&lt;short&gt; FauculityCode { get; set; }
    public Nullable&lt;short&gt; QualificationsCode { get; set; }
    public Nullable&lt;short&gt; SpecializationCode { get; set; }
    public Nullable&lt;byte&gt; EductionGrade { get; set; }
    public Nullable&lt;byte&gt; QualificationsTypeCode { get; set; }
    public Nullable&lt;byte&gt; MainEducationCode { get; set; }
    public Nullable&lt;System.DateTime&gt; GraduationYear { get; set; }
    public string Note { get; set; }
    
    public virtual tblEmployeeData tblEmployeeData { get; set; }
    public virtual tblEnglishLevel tblEnglishLevel { get; set; }
    public virtual tblFaculty tblFaculty { get; set; }
    public virtual tblMainEduaction tblMainEduaction { get; set; }
    public virtual tblQualification tblQualification { get; set; }
    public virtual tblQualificationType tblQualificationType { get; set; }
    public virtual tblSpecialization tblSpecialization { get; set; }
    public virtual tblUniversity tblUniversity { get; set; }
}

答案1

得分: 0

你可以使用:

GraduationYear = Convert.ToDateTime(dtpGraduationYear.Text.ToString());
英文:

You can use:

GraduationYear = Convert.ToDateTime(dtpGraduationYear.Text.ToString());

答案2

得分: 0

GraduationYear = dtpGraduationYear.Text.ToString("dd/MM/yyyy")

"dtpGraduationYear"你说是一个TextBox。与所有其他的文本框一样,你需要读取"Text"属性。就像错误信息所说的:

方法'tostring'的重载不接受1个参数

也就是说,"TextBox"有一个"ToString()"方法,但不接受参数。

不过,这只是答案的一部分,你的模型期望"GraduationDate"是"Nullable"类型,你需要将文本转换为该类型,可能需要使用"DateTime.Parse"或"DateTime.TryParse"。

英文:

The line with the error you said is here:

GraduationYear = dtpGraduationYear.ToString(&quot;dd/MM/yyyy&quot;)

dtpGraduationYear you said is a TextBox. As with all your other textboxes, you need to read the Text property. As the error says:

>no overload for method 'tostring' takes 1 arguments

That is, TextBox has a ToString() method, but it takes no arguments.

This is only half the answer though, your model expects GraduationDate to be Nullable&lt;DateTime&gt; - you'll need to convert the text to that type, probably using DateTime.Parse or DateTime.TryParse.

huangapple
  • 本文由 发表于 2020年1月6日 21:03:02
  • 转载请务必保留本文链接:https://go.coder-hub.com/59612623.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定