2017-02-13 62 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

相關問題