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