Database is what ghosts, how to connect, how to engage

Source: Internet
Author: User
Tags what date

Database, I have to say it is really a torture thing. In order to learn this, have to say, small made up how much brain oil, dropped how much hair, hey, say come are tears, small heart is pulled cool pull. I think, there will be a lot of programmers like me (beginner), so the small series to teach all their skills.

Lady and Gentle, male female, listen well, I'm going to do everything, BANG!!!!

The first step: Since the database connection, we certainly have a very beautiful login interface. There is not much to explain here, I am sure you will. So we focus on the database.

  private void Btnlogin_click (object sender, EventArgs e) {//Verify that the user name password matches the database//ado.net String str = "Data source=.;            Initial catalog=myschool;uid=sa;pwd=6375196 ";            SqlConnection con = new SqlConnection (str);            String sql = "SELECT COUNT (1) from student where studentname= '" + txtName.Text + "' and loginpwd= '" + txtpwd.text + "'";            SqlCommand cmd = new SqlCommand (sql, con); try {con.                Open (); int count = Convert.ToInt32 (cmd).                ExecuteScalar ());                    if (Count > 0) {//messagebox.show ("login Successful"); How to display the Frmmain interface after successful login//Hide yourself (not for anyone to remember), show others (flowers and applause), great this.                    Hide ();                    Frmmain frm = new Frmmain ();                    Frm.name = txtName.Text; frm.                Show ();  }} catch (Exception) {              MessageBox.Show ("brain Sick");//Prompt user information} finally {con.            Close ();                    The//load form was executed with private void Frmlogin_load (object sender, EventArgs e) before it was seen by the user. { }

The above is login, that is, connect. This is to log in to the database.

Interrupt a message!!! Each user using each software will certainly like an interface, is the Welcome interface:

        private void Frmmain_load (object sender, EventArgs e)        {this            . IsMdiContainer = true;            This. Text = this. Text + "Welcome you" + Name;        }   

  

The second step: we can browse to the contents of the database, and then we can add data to the database!

private void Btnedit_click (object sender, EventArgs e) {//Add student string pwd = txtpwd.text;//password String stuname = txtname.text;//name//Get gender string Stugender = string.            Empty;            if (rbtnmale.checked) {stugender = "1";            } else {Stugender = "0";            }//How to bind data to the drop-down box int gid = Getidbyname ();                            Phone string stuphone = Txtphone.text;            Address string stuaddress = Txtaddress.text;            Date datetime dt= Dpbirthday.value;            string stuemail = Txtemail.text;  Studentno, Loginpwd, Studentname, Gender, Gradeid, Phone, Address, Birthday, Email, mytt String sql = "Insert into student values (' +pwd+ ', ' +stuname+ ', ' +stugender+ ', ' +gid+ ', ' +stuphone+ ' ', ' ' +stuaddress+ ' ', ' ' +dt+ ' ', ' "+            stuemail+ "')"; String str = "Data source=.; InitiAl catalog=myschool;uid=sa;pwd=6375196 ";            SqlConnection con=new SqlConnection (str);            SqlCommand cmd=new SqlCommand (Sql,con); Con.            Open (); int count = cmd.            ExecuteNonQuery (); if (count>0) {MessageBox.Show ("Success!            "); } con.        Close (); }//This method converts the grade name into the grade number public int getidbyname () {string str = "Data source=.;        Initial catalog=myschool;uid=sa;pwd=6375196 ";        SqlConnection con = new SqlConnection (str);        String sql = "Select Gradeid from grade where Gradename= '" +cbograde.text+ "'";            SqlCommand cmd = new SqlCommand (sql, con);            int gid = 0; try {con.                Open (); Gid =convert.toint32 (cmd.            ExecuteScalar ());            } catch (Exception) {MessageBox.Show ("network exception"); } finally {con.            Close ();           } return GID; } private void Frmaddstudent_load (object sender, EventArgs e) {//Get all grade names string str = "Data source=.;            Initial catalog=myschool;uid=sa;pwd=6375196 ";            SqlConnection con = new SqlConnection (str);            String sql = "Select Gradename from Grade";            SqlCommand cmd = new SqlCommand (sql, con); try {con.               Open (); SqlDataReader dr= cmd.                ExecuteReader (); if (dr!=null) {if (Dr. HasRows) {while (Dr. Read ()) {string gname = dr["Gradename"].                            ToString ();                        Each reading to a grade name, I will "S1", add to the dropdown box CboGrade.Items.Add (gname); }}}} catch (Exception ex) {Messagebo            X.show ("network exception");    }        finally {con.            Close (); }        }

  

The third step: we can add students to the database can be searched for a certain data pull ~

 Public Frmsearchbyname () {InitializeComponent (); }//Load data in DB into ListView public void Loaddatafromdbtolistview (String sql) {string str = "Da Ta source=.;            initial catalog=myschool;uid=sa;pwd=6375196; ";                        SqlConnection con = new SqlConnection (str);            SqlCommand cmd = new SqlCommand (sql, con); try {con. Open ();//2 SqlDataReader dr = cmd.                ExecuteReader (); if (dr! = null) {if (Dr. HasRows) {while (Dr.                            Read ()) {//each read out of the DB requires a ListViewItem object that represents a row in the ListView                            int Stuno = Convert.ToInt32 (dr["Studentno"]);                            The second column stuname string stuname = Convert.ToString (dr["Studentname"]);                           Third column Sex String stugender = convert.tostring (dr["gender"]);                            Grade name String gname = Convert.ToString (dr["Gradename"]);                            ListViewItem Lvitem = new ListViewItem (stuno.tostring ());                            LVITEM.SUBITEMS.ADD (Stuname);                            LVITEM.SUBITEMS.ADD (Stugender);                            LVITEM.SUBITEMS.ADD (Gname);                        Let Lvitem and ListView correlate LvStuList.Items.Add (Lvitem); } Dr.                Close ();//-2}}} catch (Exception) {            Throw } finally {con.            Close ();//2}} private void Frmsearchbyname_load (object sender, EventArgs e) {   String sql = @ "Select Studentno,studentname,gender,gradename from Student,grade                         where Student.gradeid=grade.gradeid ";        Loaddatafromdbtolistview (SQL); } private void btnSearch_Click (object sender, EventArgs e) {//Before querying data, emptying data LV in ListView            StuList.Items.Clear ();                            String sql = @ "Select Studentno,studentname,gender,gradename from Student,grade            where Student.gradeid=grade.gradeid and studentname like '% ' +txtstudentname.text+ "% '";        Loaddatafromdbtolistview (SQL); }    }}

  

In a line!!! Part of the software will be asked to enter a birthday, what date what, then how to get it?

  Gets the value of the Calendar control          MessageBox.Show (dateTimePicker1.Value.ToShortDateString ());            if (rbomale.checked)            {              //The text box is assigned a value of male               Txtresult.text = "male";            } else if (rbofemale.checked)            {                //female                Txtresult.text = "female";            }            else            {                //And Chun, joking              txtresult.text = "Brother Chun";            }

Learn to this part of the function of the introduction is over, you may see the end will be dazed, this is right, good things to ponder ponder in Ponder, how will be a bit on the understanding, we have to think more oh

Database is what ghosts, how to connect, how to engage

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.