2017-02-13 61 views
0

美好的一天! 使用Visual Studio 2012,我创建了一个带有get和set代码的Student类,并且我需要完成StudentDAO类来创建将用于将数据存储到数据库学生表的插入代码。此操作是通过一个窗体按钮单击事件来执行的。如何使用c#编程将数据添加到MSQL数据库中?

什么,我需要创建一个按钮,单击代码然后插入到数据库代码

//Student.cs类

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 
namespace SRSJason 
{ 

class Student 
{ 
    private string S_Student_id; 
    private string S_Full_name; 
    private DateTime S_Dob; 
    private string S_Address; 
    private int S_Contact; 
    private string S_Username; 
    private string S_Password; 


    public Student()  //Default constructor 
    { 

    } 

    public Student(string Student_id, string Full_name, DateTime Dob, string Address, int Contact, string Username, string Password) //Overloadign 
    { 
     S_Student_id = Student_id; 
     S_Full_name = Full_name; 
     S_Dob = Dob; 
     S_Address = Address; 
     S_Contact = Contact; 
     S_Username = Username; 
     S_Password = Password; 
    } 

    public void setID(string Student_id) 
    { 
     S_Student_id = Student_id; 
    } 
    public string getID() 
    { 
     return S_Student_id; 
    } 

    public void setName(string Full_name) 
    { 
     S_Full_name = Full_name; 
    } 
    public string getName() 
    { 
     return S_Full_name; 
    } 

    public void setDob(DateTime Dob) 
    { 
     S_Dob = Dob; 
    } 
    public DateTime getDob() 
    { 
     return S_Dob; 
    } 

    public void setAddress(string Address) 
    { 
     S_Address = Address; 
    } 
    public string getAddress() 
    { 
     return S_Address; 
    } 

    public void setContact(int Contact) 
    { 
     S_Contact = Contact; 
    } 
    public int getContact() 
    { 
     return S_Contact; 
    } 

    public void setUsername(string Username) 
    { 
     S_Username = Username; 
    } 
    public string getUsername() 
    { 
     return S_Username; 
    } 

    public void setPassword(string Password) 
    { 
     S_Password = Password; 
    } 
    public string getPassword() 
    { 
     return S_Password; 
    } 




} 

}`

// StudentDAO (请帮我完成此代码)

`class StudentDAO 
{ 
    static string constring = "Data Source=JAZE;Initial Catalog=srsjason;Integrated Security=True"; 
    SqlConnection m_con = new SqlConnection(constring); 


}` 
从形式

//点击按钮(请帮我完成这个代码以及)

private void submitstudent(object sender, EventArgs e) 
    { 


    } 

请帮助我,当你创建私有属性来完成这个编码

+0

http://meta.softwareengineering.stackexchange.com/questions/6166/open-letter-to-students-with-homework-problems –

回答

0

首先,ü不能访问他们在你的形式中,你必须在同一个类中创建一个方法,然后在你的表单中使用它。其次你应该知道你正在使用的ORM - Object Relational Mapping

在这里,我将列出他们:

当您选择其中的一个。下一步就是了解他们如何工作以及语法是什么。

但是知道你有点显示的语法ADO.NET下面是一个示例,说明如何使用ADO.NET在数据库中插入数据。如果你想在没有方法的情况下直接从代码隐藏中添加数据。所以基本上你的按钮的点击事件。

private void btn_Click(object sender, EventArgs e) 
{ 
    try 
    { 
    //create object of Connection Class.................. 
    SqlConnection con = new SqlConnection(); 

    // Set Connection String property of Connection object.................. 
    con.ConnectionString = "Data Source=KUSH-PC;Initial Catalog=test;Integrated   Security=True"; 

// Open Connection.................. 
    con.Open(); 

//Create object of Command Class................ 
SqlCommand cmd = new SqlCommand(); 

//set Connection Property of Command object............. 
cmd.Connection = con; 
//Set Command type of command object 
//1.StoredProcedure 
//2.TableDirect 
//3.Text (By Default) 

cmd.CommandType = CommandType.Text; 

//Set Command text Property of command object......... 

cmd.CommandText = "Insert into Registration (Username, password) values ('@user','@pass')"; 

//Assign values as `parameter`. It avoids `SQL Injection` 
cmd.Parameters.AddWithValue("user", TextBox1.text); 
cmd.Parameters.AddWithValue("pass", TextBox2.text); 

     //Execute command by calling following method................ 
    //1.ExecuteNonQuery() 
     //This is used for insert,delete,update command........... 
    //2.ExecuteScalar() 
     //This returns a single value .........(used only for select command) 
    //3.ExecuteReader() 

     //Return one or more than one record. 
    cmd.ExecuteNonQuery(); 
    con.Close(); 


    MessageBox.Show("Data Saved");   
    } 
    catch (Exception ex) 
    { 
      MessageBox.Show(ex.Message); 
      con.Close(); 
    } 


    } 

确保您已在config文件中包含您的ConnectionString

+0

谢谢队友,我会试试这个:),非常感谢 –

+0

@RichardJSimmons没问题:) – Valkyrie

相关问题