使用带有类的c#将数据插入MS Access

时间:2019-06-05 07:33:37

标签: c# ms-access

我想使用c#将数据插入到Microsoft访问中,并且需要使用类。

我尝试删除一些文本框并查找一些错误,我认为IF条件可能存在一些错误。

private void registerbutton_Click(object sender, EventArgs e)
{
    -- CLASS --
    RegStudent reg = new RegStudent();
    reg.stdfname = fnametextbox.Text;
    reg.stdlname = lnametextbox.Text;
    reg.username = usernametextbox.Text;
    reg.password = passwordtextbox.Text;
    reg.dob = dobtextbox.Text;
    reg.city = citytextbox.Text;
    reg.state = statetextbox.Text;
    reg.email = emailtextbox.Text;
    reg.phoneno = ctcnotextbox.Text;
    reg.phoneno2 = ctcnotextbox2.Text;
    reg.course = coursetextbox.Text;
    reg.emergencyname = emergencynametextbox.Text;
    reg.emergencyphoneno = emergencynumbertextbox.Text;
    reg.registerdate = registerdatetextbox.Text;
    if (tptextbox.Text != "" && fnametextbox.Text != "" && lnametextbox.Text != "" && dobtextbox.Text != "" && usernametextbox.Text != "" && passwordtextbox.Text != "" && citytextbox.Text != "" && statetextbox.Text != "" && registerdatetextbox.Text != "" && emailtextbox.Text != "" && ctcnotextbox.Text != "" && ctcnotextbox2.Text != "" && coursetextbox.Text != "" && emergencynametextbox.Text != "" && emergencynumbertextbox.Text != "")
    {
        registerconnection.Open();
        OleDbCommand insert = new OleDbCommand();
        insert.Connection = registerconnection;
        insert.CommandText = "Insert into StudentDatabase values (" + reg.stdfname + "','" + reg.stdlname + "','" + reg.username + "','" + reg.password + "','" + reg.dob + "','" + reg.city + "','" + reg.state + "','" + reg.email + "','" + reg.phoneno + "','" + reg.phoneno2 + "','" + reg.course + "','" + reg.emergencyname + "','" + reg.emergencyphoneno + ");";
        insert.CommandType = CommandType.Text;
        insert.Connection = registerconnection;
        insert.ExecuteNonQuery();
        MessageBox.Show("Data Have Been Registered.");
    }
    else
    {
        MessageBox.Show("error");
    }
}

我希望输出将是保存数据。

0 个答案:

没有答案