C # call Server_SQL

C # call Server_SQL:
the SqlCommand: Database command statement
// database path
static String GetConnectString ()
{
  return "Data Source = 192.168.0.96; Initial Catalog = haha; User ID = SA; Zhao pwd =";
}

Inserting database information
Private void InsetSQL ()
{
  the using (Conn the SqlConnection the SqlConnection new new = (GetConnectString ()))
  {
    // Open Database
    conn.Open ();

    //插入数据
    string str = "INSERT INTO[haha].[dbo].[student] VALUES('wwww', '111', 'hahhhh')";
    SqlCommand sqlc = new SqlCommand(str, conn);

    // number of operation
    label1.Text = sqlc.ExecuteNonQuery () ToString () .;

    // Close the connection
    conn.Close ();
  }
}

// query the database information label display
Private void SelectSQL ()
{
  the using (Conn the SqlConnection the SqlConnection new new = (GetConnectString ()))
  {
  // Open Database
  conn.Open ();

  // Create a database command
  SqlCommand cmd = conn.CreateCommand ();

  // Create a query
  cmd.CommandText = "SELECT * FROM [haha ] [dbo] [student]..";

  // read from the database into a data stream reader in
  the SqlDataReader reader cmd.ExecuteReader = ();
  // data from the reader reads the next line, if there is no data, reader.Read () Returns flase
  the while (reader.Read ( ))
  {
      String name = reader.GetString (reader.GetOrdinal ( "name"));
      int = reader.GetInt32 Age (reader.GetOrdinal ( "Age"));
      String = reader.GetString Gender (reader.GetOrdinal ( "Gender "));
      // the output data format
      Label2.Text = Label2.Text +" \ n-'+ (name + "," + Age + "," + Gender);
  }

  // Close the connection
  conn.Close ();
  }
}

Delete database information
Private void InsetSQL ()
{

  the using (Conn the SqlConnection the SqlConnection new new = (GetConnectString ()))
  {
    // Open Database
    conn.Open ();

    // delete data operation command
    string sql = "DELETE FROM haha.dbo.student where name = 'wwww'";

    SqlCommand cmd = conn.CreateCommand();
    cmd.CommandText = sql;

    label3.Text = cmd.ExecuteNonQuery().ToString();

    // Close the connection
    conn.Close ();
  }
}

Guess you like

Origin www.cnblogs.com/XiaoLang0/p/11573685.html