Summary of database making 1

Source: Internet
Author: User

Connect to the SQL database in C #.
The code is as follows:

String sconn = @ "Data source=.; Initial Catalog=dbs; Integrated Security=true ";

SqlConnection conn = new SqlConnection (sconn);

Create a string that contains information about the database. Data Source =.             Represents a database as a local database. Initial Catalog=dbs indicates that the database is connected.
Put in a Botton_click function:

private void Btngetdata_click (object sender, EventArgs e)

{

The teacher put in each Botton_click the database connection thing;

SqlDataReader reader = cmd. ExecuteReader ();

ListBox1.Items.Clear ();

while (reader. Read ())

{

String Sno = reader["Sno"]. ToString (). Trim ();

String Sname = reader["Sname"]. ToString (). Trim ();

String Ssex = Convert.isdbnull (reader["Ssex"])? "": reader["Ssex"]. ToString (). Trim ();

int Sage = Convert.isdbnull (reader["Sage"])? 20:convert.toint32 (reader["Sage");

String sdept = Convert.isdbnull (reader["sdept"])? "": reader["sdept"]. ToString (). Trim ();
string s = String. Format ("no:{0}, Name:{1}, sex:{2}, Age:{3}, Dept:{4}", Sno, Sname, Ssex, Sage, sdept);

LISTBOX1.ITEMS.ADD (s);

}

Reader. Close ();

The IsDBNull method tests whether the value parameter equals dbnullvalue. It is equivalent to the following code:

Return DBNull.Value.Equals (Value)

Summary of database making 1

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.