2017-02-13 5 views
0

おはよう! ビジュアルスタジオ2012を使用して、getおよびsetコードを使用してStudentクラスを作成しました。私は、StudentDAOクラスを完了して、データベース学生テーブルにデータを格納するための挿入コーディングを作成する必要があります。このアクションは、Windowsフォームボタンのクリックイベントによって実行されます。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

ファーストを完了するために私を助けてくださいuのカントのアクセスあなたのフォームの中にある場合は、代わりに同じクラスの中にメソッドを作成してからフォームに使用する必要があります。次に使用している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

関連する問題